blob: 61536157b81e4b470bc25d7a437b181d9ed590a2 [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.
Colin Crossdcf71b22021-02-01 13:59:03 -080063 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010064}
65
66// Extract tag specific information from the dependency.
67func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080068 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010069 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
Anton Hansson08f476b2021-04-07 15:32:19 +0100204func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
205 return ".stubs" + scope.moduleSuffix
206}
207
Paul Duffinc3091c82020-05-08 14:16:20 +0100208func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100209 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000210}
211
Paul Duffinc8782502020-04-29 20:45:27 +0100212func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100213 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000214}
215
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100216func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100217 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100218}
219
Paul Duffin3375e352020-04-28 10:44:03 +0100220func (scope *apiScope) String() string {
221 return scope.name
222}
223
Paul Duffind1b3a922020-01-22 11:57:20 +0000224type apiScopes []*apiScope
225
226func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
227 var list []string
228 for _, scope := range scopes {
229 list = append(list, accessor(scope))
230 }
231 return list
232}
233
Jiyong Parkc678ad32018-04-10 13:07:10 +0900234var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100235 scopeByName = make(map[string]*apiScope)
236 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000237 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100238 name: "public",
239
240 // Public scope is enabled by default for both legacy and non-legacy modes.
241 legacyEnabledStatus: func(module *SdkLibrary) bool {
242 return true
243 },
244 defaultEnabledStatus: true,
245
246 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
247 return &module.sdkLibraryProperties.Public
248 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000249 sdkVersion: "current",
250 })
251 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100252 name: "system",
253 extends: apiScopePublic,
254 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
255 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
256 return &module.sdkLibraryProperties.System
257 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100258 apiFilePrefix: "system-",
259 moduleSuffix: ".system",
260 sdkVersion: "system_current",
261 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000262 })
263 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100264 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100265 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100266 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
267 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
268 return &module.sdkLibraryProperties.Test
269 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100270 apiFilePrefix: "test-",
271 moduleSuffix: ".test",
272 sdkVersion: "test_current",
273 annotation: "android.annotation.TestApi",
274 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000275 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100276 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100277 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100278 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100279 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100280 //
281 // Enabling this would break existing usages.
282 legacyEnabledStatus: func(module *SdkLibrary) bool {
283 return false
284 },
285 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
286 return &module.sdkLibraryProperties.Module_lib
287 },
288 apiFilePrefix: "module-lib-",
289 moduleSuffix: ".module_lib",
290 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100291 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100292 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100293 apiScopeSystemServer = initApiScope(&apiScope{
294 name: "system-server",
295 extends: apiScopePublic,
296 // The system-server scope is disabled by default in legacy mode.
297 //
298 // Enabling this would break existing usages.
299 legacyEnabledStatus: func(module *SdkLibrary) bool {
300 return false
301 },
302 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
303 return &module.sdkLibraryProperties.System_server
304 },
305 apiFilePrefix: "system-server-",
306 moduleSuffix: ".system_server",
307 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100308 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
309 extraArgs: []string{
310 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100311 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100312 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100313 },
314 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000315 allApiScopes = apiScopes{
316 apiScopePublic,
317 apiScopeSystem,
318 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100319 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100320 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000321 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900322)
323
Jiyong Park82484c02018-04-23 21:41:26 +0900324var (
325 javaSdkLibrariesLock sync.Mutex
326)
327
Jiyong Parkc678ad32018-04-10 13:07:10 +0900328// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900329// 1) disallowing linking to the runtime shared lib
330// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900331
332func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000333 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900334
Jiyong Park82484c02018-04-23 21:41:26 +0900335 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
336 javaSdkLibraries := javaSdkLibraries(ctx.Config())
337 sort.Strings(*javaSdkLibraries)
338 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
339 })
Paul Duffindd46f712020-02-10 13:37:10 +0000340
341 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100342 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900343}
344
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000345func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
346 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
347 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
348}
349
Paul Duffin3375e352020-04-28 10:44:03 +0100350// Properties associated with each api scope.
351type ApiScopeProperties struct {
352 // Indicates whether the api surface is generated.
353 //
354 // If this is set for any scope then all scopes must explicitly specify if they
355 // are enabled. This is to prevent new usages from depending on legacy behavior.
356 //
357 // Otherwise, if this is not set for any scope then the default behavior is
358 // scope specific so please refer to the scope specific property documentation.
359 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100360
361 // The sdk_version to use for building the stubs.
362 //
363 // If not specified then it will use an sdk_version determined as follows:
364 // 1) If the sdk_version specified on the java_sdk_library is none then this
365 // will be none. This is used for java_sdk_library instances that are used
366 // to create stubs that contribute to the core_current sdk version.
367 // 2) Otherwise, it is assumed that this library extends but does not contribute
368 // directly to a specific sdk_version and so this uses the sdk_version appropriate
369 // for the api scope. e.g. public will use sdk_version: current, system will use
370 // sdk_version: system_current, etc.
371 //
372 // This does not affect the sdk_version used for either generating the stubs source
373 // or the API file. They both have to use the same sdk_version as is used for
374 // compiling the implementation library.
375 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100376}
377
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378type sdkLibraryProperties struct {
Paul Duffin5df79302020-05-16 15:52:12 +0100379 // Visibility for impl library module. If not specified then defaults to the
380 // visibility property.
381 Impl_library_visibility []string
382
Paul Duffin4911a892020-04-29 23:35:13 +0100383 // Visibility for stubs library modules. If not specified then defaults to the
384 // visibility property.
385 Stubs_library_visibility []string
386
387 // Visibility for stubs source modules. If not specified then defaults to the
388 // visibility property.
389 Stubs_source_visibility []string
390
Anton Hansson7f66efa2020-10-08 14:47:23 +0100391 // List of Java libraries that will be in the classpath when building the implementation lib
392 Impl_only_libs []string `android:"arch_variant"`
393
Sundong Ahnf043cf62018-06-25 16:04:37 +0900394 // List of Java libraries that will be in the classpath when building stubs
395 Stub_only_libs []string `android:"arch_variant"`
396
Anton Hanssondae54cd2021-04-21 16:30:10 +0100397 // List of Java libraries that will included in stub libraries
398 Stub_only_static_libs []string `android:"arch_variant"`
399
Paul Duffin7a586d32019-12-30 17:09:34 +0000400 // list of package names that will be documented and publicized as API.
401 // This allows the API to be restricted to a subset of the source files provided.
402 // If this is unspecified then all the source files will be treated as being part
403 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900404 Api_packages []string
405
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900406 // list of package names that must be hidden from the API
407 Hidden_api_packages []string
408
Paul Duffin749f98f2019-12-30 17:23:46 +0000409 // the relative path to the directory containing the api specification files.
410 // Defaults to "api".
411 Api_dir *string
412
Paul Duffindfa131e2020-05-15 20:37:11 +0100413 // Determines whether a runtime implementation library is built; defaults to false.
414 //
415 // If true then it also prevents the module from being used as a shared module, i.e.
416 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000417 Api_only *bool
418
Paul Duffin11512472019-02-11 15:55:17 +0000419 // local files that are used within user customized droiddoc options.
420 Droiddoc_option_files []string
421
422 // additional droiddoc options
423 // Available variables for substitution:
424 //
425 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900426 Droiddoc_options []string
427
Paul Duffine22c2ab2020-05-20 19:35:27 +0100428 // is set to true, Metalava will allow framework SDK to contain annotations.
429 Annotations_enabled *bool
430
Sundong Ahn054b19a2018-10-19 13:46:09 +0900431 // a list of top-level directories containing files to merge qualifier annotations
432 // (i.e. those intended to be included in the stubs written) from.
433 Merge_annotations_dirs []string
434
435 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
436 Merge_inclusion_annotations_dirs []string
437
438 // If set to true, the path of dist files is apistubs/core. Defaults to false.
439 Core_lib *bool
440
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000441 // If set to true then don't create dist rules.
442 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900443
Paul Duffin31310252020-11-20 21:26:20 +0000444 // The stem for the artifacts that are copied to the dist, if not specified
445 // then defaults to the base module name.
446 //
447 // For each scope the following artifacts are copied to the apistubs/<scope>
448 // directory in the dist.
449 // * stubs impl jar -> <dist-stem>.jar
450 // * API specification file -> api/<dist-stem>.txt
451 // * Removed API specification file -> api/<dist-stem>-removed.txt
452 //
453 // Also used to construct the name of the filegroup (created by prebuilt_apis)
454 // that references the latest released API and remove API specification files.
455 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
456 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800457 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000458 Dist_stem *string
459
Anton Hanssondff2c782020-12-21 17:10:01 +0000460 // A compatibility mode that allows historical API-tracking files to not exist.
461 // Do not use.
462 Unsafe_ignore_missing_latest_api bool
463
Paul Duffin3375e352020-04-28 10:44:03 +0100464 // indicates whether system and test apis should be generated.
465 Generate_system_and_test_apis bool `blueprint:"mutated"`
466
467 // The properties specific to the public api scope
468 //
469 // Unless explicitly specified by using public.enabled the public api scope is
470 // enabled by default in both legacy and non-legacy mode.
471 Public ApiScopeProperties
472
473 // The properties specific to the system api scope
474 //
475 // In legacy mode the system api scope is enabled by default when sdk_version
476 // is set to something other than "none".
477 //
478 // In non-legacy mode the system api scope is disabled by default.
479 System ApiScopeProperties
480
481 // The properties specific to the test api scope
482 //
483 // In legacy mode the test api scope is enabled by default when sdk_version
484 // is set to something other than "none".
485 //
486 // In non-legacy mode the test api scope is disabled by default.
487 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000488
Paul Duffin0c5bae52020-06-02 13:00:08 +0100489 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100490 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100491 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100492 // disabled by default.
493 Module_lib ApiScopeProperties
494
Paul Duffin0c5bae52020-06-02 13:00:08 +0100495 // The properties specific to the system-server api scope
496 //
497 // Unless explicitly specified by using test.enabled the module-lib api scope is
498 // disabled by default.
499 System_server ApiScopeProperties
500
Jiyong Park932cdfe2020-05-28 00:19:53 +0900501 // Determines if the stubs are preferred over the implementation library
502 // for linking, even when the client doesn't specify sdk_version. When this
503 // is set to true, such clients are provided with the widest API surface that
504 // this lib provides. Note however that this option doesn't affect the clients
505 // that are in the same APEX as this library. In that case, the clients are
506 // always linked with the implementation library. Default is false.
507 Default_to_stubs *bool
508
Paul Duffin160fe412020-05-10 19:32:20 +0100509 // Properties related to api linting.
510 Api_lint struct {
511 // Enable api linting.
512 Enabled *bool
513 }
514
Jiyong Parkc678ad32018-04-10 13:07:10 +0900515 // TODO: determines whether to create HTML doc or not
516 //Html_doc *bool
517}
518
Paul Duffin0f8faff2020-05-20 16:18:00 +0100519// Paths to outputs from java_sdk_library and java_sdk_library_import.
520//
521// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
522// OptionalPaths are always set by java_sdk_library but may not be set by
523// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000524type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100525 // The path (represented as Paths for convenience when returning) to the stubs header jar.
526 //
527 // That is the jar that is created by turbine.
528 stubsHeaderPath android.Paths
529
530 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
531 //
532 // This is not the implementation jar, it still only contains stubs.
533 stubsImplPath android.Paths
534
Paul Duffin1267d872021-04-16 17:21:36 +0100535 // The dex jar for the stubs.
536 //
537 // This is not the implementation jar, it still only contains stubs.
538 stubsDexJarPath android.Path
539
Paul Duffin0f8faff2020-05-20 16:18:00 +0100540 // The API specification file, e.g. system_current.txt.
541 currentApiFilePath android.OptionalPath
542
543 // The specification of API elements removed since the last release.
544 removedApiFilePath android.OptionalPath
545
546 // The stubs source jar.
547 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000548}
549
Colin Crossdcf71b22021-02-01 13:59:03 -0800550func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
551 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
552 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
553 paths.stubsHeaderPath = lib.HeaderJars
554 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100555
556 libDep := dep.(UsesLibraryDependency)
557 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100558 return nil
559 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800560 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100561 }
562}
563
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100564func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
565 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
566 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100567 return nil
568 } else {
569 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
570 }
571}
572
Paul Duffin0f8faff2020-05-20 16:18:00 +0100573func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
574 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
575 action(apiStubsProvider)
576 return nil
577 } else {
578 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
579 }
580}
581
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100582func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100583 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
584 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100585}
586
Colin Crossdcf71b22021-02-01 13:59:03 -0800587func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100588 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
589 paths.extractApiInfoFromApiStubsProvider(provider)
590 })
591}
592
Paul Duffin0f8faff2020-05-20 16:18:00 +0100593func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
594 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100595}
596
Colin Crossdcf71b22021-02-01 13:59:03 -0800597func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100598 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100599 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
600 })
601}
602
Colin Crossdcf71b22021-02-01 13:59:03 -0800603func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100604 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
605 paths.extractApiInfoFromApiStubsProvider(provider)
606 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
607 })
608}
609
610type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100611 // The naming scheme to use for the components that this module creates.
612 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100613 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100614 //
615 // This is a temporary mechanism to simplify conversion from separate modules for each
616 // component that follow a different naming pattern to the default one.
617 //
618 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100619 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100620
621 // Specifies whether this module can be used as an Android shared library; defaults
622 // to true.
623 //
624 // An Android shared library is one that can be referenced in a <uses-library> element
625 // in an AndroidManifest.xml.
626 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100627
628 // Files containing information about supported java doc tags.
629 Doctag_files []string `android:"path"`
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100630}
631
Paul Duffin56d44902020-01-31 13:36:25 +0000632// Common code between sdk library and sdk library import
633type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100634 moduleBase *android.ModuleBase
635
Paul Duffin56d44902020-01-31 13:36:25 +0000636 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100637
638 namingScheme sdkLibraryComponentNamingScheme
639
Paul Duffindfa131e2020-05-15 20:37:11 +0100640 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100641
Paul Duffina2ae7e02020-09-11 11:55:00 +0100642 // Paths to commonSdkLibraryProperties.Doctag_files
643 doctagPaths android.Paths
644
Paul Duffin859fe962020-05-15 10:20:31 +0100645 // Functionality related to this being used as a component of a java_sdk_library.
646 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000647}
648
Paul Duffinc3091c82020-05-08 14:16:20 +0100649func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
650 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100651
Paul Duffindfa131e2020-05-15 20:37:11 +0100652 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100653
654 // Initialize this as an sdk library component.
655 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100656}
657
658func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100659 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100660 switch schemeProperty {
661 case "default":
662 c.namingScheme = &defaultNamingScheme{}
663 default:
664 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
665 return false
666 }
667
Paul Duffindfa131e2020-05-15 20:37:11 +0100668 // Only track this sdk library if this can be used as a shared library.
669 if c.sharedLibrary() {
670 // Use the name specified in the module definition as the owner.
671 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
672 }
Paul Duffin859fe962020-05-15 10:20:31 +0100673
Paul Duffin1b1e8062020-05-08 13:44:43 +0100674 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100675}
676
Paul Duffina2ae7e02020-09-11 11:55:00 +0100677func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
678 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
679}
680
Paul Duffineedc5d52020-06-12 17:46:39 +0100681// Module name of the runtime implementation library
682func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
683 return c.moduleBase.BaseModuleName() + ".impl"
684}
685
686// Module name of the XML file for the lib
687func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
688 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
689}
690
Paul Duffinc3091c82020-05-08 14:16:20 +0100691// Name of the java_library module that compiles the stubs source.
692func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100693 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100694}
695
696// Name of the droidstubs module that generates the stubs source and may also
697// generate/check the API.
698func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100699 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100700}
701
702// Name of the droidstubs module that generates/checks the API. Only used if it
703// requires different arts to the stubs source generating module.
704func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100705 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100706}
707
Paul Duffin46dc45a2020-05-14 15:39:10 +0100708// The component names for different outputs of the java_sdk_library.
709//
710// They are similar to the names used for the child modules it creates
711const (
712 stubsSourceComponentName = "stubs.source"
713
714 apiTxtComponentName = "api.txt"
715
716 removedApiTxtComponentName = "removed-api.txt"
717)
718
719// A regular expression to match tags that reference a specific stubs component.
720//
721// It will only match if given a valid scope and a valid component. It is verfy strict
722// to ensure it does not accidentally match a similar looking tag that should be processed
723// by the embedded Library.
724var tagSplitter = func() *regexp.Regexp {
725 // Given a list of literal string items returns a regular expression that will
726 // match any one of the items.
727 choice := func(items ...string) string {
728 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
729 }
730
731 // Regular expression to match one of the scopes.
732 scopesRegexp := choice(allScopeNames...)
733
734 // Regular expression to match one of the components.
735 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
736
737 // Regular expression to match any combination of one scope and one component.
738 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
739}()
740
741// For OutputFileProducer interface
742//
743// .<scope>.stubs.source
744// .<scope>.api.txt
745// .<scope>.removed-api.txt
746func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
747 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
748 scopeName := groups[1]
749 component := groups[2]
750
751 if scope, ok := scopeByName[scopeName]; ok {
752 paths := c.findScopePaths(scope)
753 if paths == nil {
754 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
755 }
756
757 switch component {
758 case stubsSourceComponentName:
759 if paths.stubsSrcJar.Valid() {
760 return android.Paths{paths.stubsSrcJar.Path()}, nil
761 }
762
763 case apiTxtComponentName:
764 if paths.currentApiFilePath.Valid() {
765 return android.Paths{paths.currentApiFilePath.Path()}, nil
766 }
767
768 case removedApiTxtComponentName:
769 if paths.removedApiFilePath.Valid() {
770 return android.Paths{paths.removedApiFilePath.Path()}, nil
771 }
772 }
773
774 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
775 } else {
776 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
777 }
778
779 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100780 switch tag {
781 case ".doctags":
782 if c.doctagPaths != nil {
783 return c.doctagPaths, nil
784 } else {
785 return nil, fmt.Errorf("no doctag_files specified on %s", c.moduleBase.BaseModuleName())
786 }
787 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100788 return nil, nil
789 }
790}
791
Paul Duffin803a9562020-05-20 11:52:25 +0100792func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000793 if c.scopePaths == nil {
794 c.scopePaths = make(map[*apiScope]*scopePaths)
795 }
796 paths := c.scopePaths[scope]
797 if paths == nil {
798 paths = &scopePaths{}
799 c.scopePaths[scope] = paths
800 }
801
802 return paths
803}
804
Paul Duffin803a9562020-05-20 11:52:25 +0100805func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
806 if c.scopePaths == nil {
807 return nil
808 }
809
810 return c.scopePaths[scope]
811}
812
813// If this does not support the requested api scope then find the closest available
814// scope it does support. Returns nil if no such scope is available.
815func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
816 for s := scope; s != nil; s = s.extends {
817 if paths := c.findScopePaths(s); paths != nil {
818 return paths
819 }
820 }
821
822 // This should never happen outside tests as public should be the base scope for every
823 // scope and is enabled by default.
824 return nil
825}
826
Jiyong Parkf1691d22021-03-29 20:11:58 +0900827func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100828
829 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900830 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffinb05d4292020-05-20 12:19:10 +0100831 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
832 }
833
Paul Duffin1267d872021-04-16 17:21:36 +0100834 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
835 if paths == nil {
836 return nil
837 }
838
839 return paths.stubsHeaderPath
840}
841
842// selectScopePaths returns the *scopePaths appropriate for the specific kind.
843//
844// If the module does not support the specific kind then it will return the *scopePaths for the
845// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
846// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
847func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100848 var apiScope *apiScope
Paul Duffin1267d872021-04-16 17:21:36 +0100849 switch kind {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900850 case android.SdkSystem:
Paul Duffinb05d4292020-05-20 12:19:10 +0100851 apiScope = apiScopeSystem
Jiyong Parkf1691d22021-03-29 20:11:58 +0900852 case android.SdkModule:
Paul Duffin803a9562020-05-20 11:52:25 +0100853 apiScope = apiScopeModuleLib
Jiyong Parkf1691d22021-03-29 20:11:58 +0900854 case android.SdkTest:
Paul Duffinb05d4292020-05-20 12:19:10 +0100855 apiScope = apiScopeTest
Jiyong Parkf1691d22021-03-29 20:11:58 +0900856 case android.SdkSystemServer:
Paul Duffin0c5bae52020-06-02 13:00:08 +0100857 apiScope = apiScopeSystemServer
Paul Duffinb05d4292020-05-20 12:19:10 +0100858 default:
859 apiScope = apiScopePublic
860 }
861
Paul Duffin803a9562020-05-20 11:52:25 +0100862 paths := c.findClosestScopePath(apiScope)
863 if paths == nil {
864 var scopes []string
865 for _, s := range allApiScopes {
866 if c.findScopePaths(s) != nil {
867 scopes = append(scopes, s.name)
868 }
869 }
870 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
871 return nil
872 }
873
Paul Duffin1267d872021-04-16 17:21:36 +0100874 return paths
875}
876
877// to satisfy SdkLibraryDependency interface
878func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) android.Path {
879 paths := c.selectScopePaths(ctx, kind)
880 if paths == nil {
881 return nil
882 }
883
884 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100885}
886
Paul Duffin859fe962020-05-15 10:20:31 +0100887func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
888 componentProps := &struct {
889 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100890 }{}
891
892 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100893 // Mark the stubs library as being components of this java_sdk_library so that
894 // any app that includes code which depends (directly or indirectly) on the stubs
895 // library will have the appropriate <uses-library> invocation inserted into its
896 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100897 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100898 }
899
900 return componentProps
901}
902
Paul Duffindfa131e2020-05-15 20:37:11 +0100903func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
904 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
905}
906
Paul Duffinf4600f62021-05-13 22:34:45 +0100907// Check if the stub libraries should be compiled for dex
908func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
909 // Always compile the dex file files for the stub libraries if they will be used on the
910 // bootclasspath.
911 return !c.sharedLibrary()
912}
913
Paul Duffin859fe962020-05-15 10:20:31 +0100914// Properties related to the use of a module as an component of a java_sdk_library.
915type SdkLibraryComponentProperties struct {
916
917 // The name of the java_sdk_library/_import to add to a <uses-library> entry
918 // in the AndroidManifest.xml of any Android app that includes code that references
919 // this module. If not set then no java_sdk_library/_import is tracked.
920 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
921}
922
923// Structure to be embedded in a module struct that needs to support the
924// SdkLibraryComponentDependency interface.
925type EmbeddableSdkLibraryComponent struct {
926 sdkLibraryComponentProperties SdkLibraryComponentProperties
927}
928
929func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
930 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
931}
932
933// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100934func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
935 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
Paul Duffin859fe962020-05-15 10:20:31 +0100936}
937
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100938// to satisfy SdkLibraryComponentDependency
939func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
940 // Currently implementation library name is the same as the SDK library name.
941 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
942}
943
Paul Duffin859fe962020-05-15 10:20:31 +0100944// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
945// (including the java_sdk_library) itself.
946type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100947 UsesLibraryDependency
948
Paul Duffin859fe962020-05-15 10:20:31 +0100949 // The optional name of the sdk library that should be implicitly added to the
950 // AndroidManifest of an app that contains code which references the sdk library.
951 //
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100952 // Returns the name of the optional implicit SDK library or nil, if there isn't one.
953 OptionalImplicitSdkLibrary() *string
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100954
955 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
956 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +0100957}
958
959// Make sure that all the module types that are components of java_sdk_library/_import
960// and which can be referenced (directly or indirectly) from an android app implement
961// the SdkLibraryComponentDependency interface.
962var _ SdkLibraryComponentDependency = (*Library)(nil)
963var _ SdkLibraryComponentDependency = (*Import)(nil)
964var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100965var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +0100966
967// Provides access to sdk_version related header and implentation jars.
968type SdkLibraryDependency interface {
969 SdkLibraryComponentDependency
970
971 // Get the header jars appropriate for the supplied sdk_version.
972 //
973 // These are turbine generated jars so they only change if the externals of the
974 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +0900975 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +0100976
977 // Get the implementation jars appropriate for the supplied sdk version.
978 //
979 // These are either the implementation jar for the whole sdk library or the implementation
980 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
981 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +0900982 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +0100983
984 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
985 // tool which processes dex files.
986 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) android.Path
Paul Duffinf4600f62021-05-13 22:34:45 +0100987
988 // sharedLibrary returns true if this can be used as a shared library.
989 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +0100990}
991
Inseob Kimc0907f12019-02-08 21:00:45 +0900992type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900993 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900994
Sundong Ahn054b19a2018-10-19 13:46:09 +0900995 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900996
Paul Duffin3375e352020-04-28 10:44:03 +0100997 // Map from api scope to the scope specific property structure.
998 scopeToProperties map[*apiScope]*ApiScopeProperties
999
Paul Duffin56d44902020-01-31 13:36:25 +00001000 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001001}
1002
Inseob Kimc0907f12019-02-08 21:00:45 +09001003var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001004
Paul Duffin3375e352020-04-28 10:44:03 +01001005func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1006 return module.sdkLibraryProperties.Generate_system_and_test_apis
1007}
1008
1009func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1010 // Check to see if any scopes have been explicitly enabled. If any have then all
1011 // must be.
1012 anyScopesExplicitlyEnabled := false
1013 for _, scope := range allApiScopes {
1014 scopeProperties := module.scopeToProperties[scope]
1015 if scopeProperties.Enabled != nil {
1016 anyScopesExplicitlyEnabled = true
1017 break
1018 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001019 }
Paul Duffin3375e352020-04-28 10:44:03 +01001020
1021 var generatedScopes apiScopes
1022 enabledScopes := make(map[*apiScope]struct{})
1023 for _, scope := range allApiScopes {
1024 scopeProperties := module.scopeToProperties[scope]
1025 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1026 // This is to ensure that any new usages of this module type do not rely on legacy
1027 // behaviour.
1028 defaultEnabledStatus := false
1029 if anyScopesExplicitlyEnabled {
1030 defaultEnabledStatus = scope.defaultEnabledStatus
1031 } else {
1032 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1033 }
1034 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1035 if enabled {
1036 enabledScopes[scope] = struct{}{}
1037 generatedScopes = append(generatedScopes, scope)
1038 }
1039 }
1040
1041 // Now check to make sure that any scope that is extended by an enabled scope is also
1042 // enabled.
1043 for _, scope := range allApiScopes {
1044 if _, ok := enabledScopes[scope]; ok {
1045 extends := scope.extends
1046 if extends != nil {
1047 if _, ok := enabledScopes[extends]; !ok {
1048 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1049 }
1050 }
1051 }
1052 }
1053
1054 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001055}
1056
Paul Duffineedc5d52020-06-12 17:46:39 +01001057type sdkLibraryComponentTag struct {
1058 blueprint.BaseDependencyTag
1059 name string
1060}
1061
1062// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1063func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1064
1065var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001066
Jiyong Parke3833882020-02-17 17:28:10 +09001067func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001068 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001069 return dt == xmlPermissionsFileTag
1070 }
1071 return false
1072}
1073
Paul Duffineedc5d52020-06-12 17:46:39 +01001074var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001075
Paul Duffin44f1d842020-06-26 20:17:02 +01001076// Add the dependencies on the child modules in the component deps mutator.
1077func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001078 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001079 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001080 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001081
Paul Duffin15f34ef2020-07-20 18:04:44 +01001082 // Add a dependency on the stubs source in order to access both stubs source and api information.
1083 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001084 }
1085
Paul Duffindfa131e2020-05-15 20:37:11 +01001086 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001087 // Add dependency to the rule for generating the implementation library.
1088 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1089
Paul Duffindfa131e2020-05-15 20:37:11 +01001090 if module.sharedLibrary() {
1091 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001092 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001093 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001094 }
1095}
Paul Duffine74ac732020-02-06 13:51:46 +00001096
Paul Duffin44f1d842020-06-26 20:17:02 +01001097// Add other dependencies as normal.
1098func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001099 var missingApiModules []string
1100 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1101 if apiScope.unstable {
1102 continue
1103 }
1104 if m := android.SrcIsModule(module.latestApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1105 missingApiModules = append(missingApiModules, m)
1106 }
1107 if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1108 missingApiModules = append(missingApiModules, m)
1109 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001110 if m := android.SrcIsModule(module.latestIncompatibilitiesFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1111 missingApiModules = append(missingApiModules, m)
1112 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001113 }
1114 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1115 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1116 m += "You need to do one of the following:\n"
1117 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1118 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1119 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1120 m += "\n"
1121 m += "The following filegroup modules are missing:\n "
1122 m += strings.Join(missingApiModules, "\n ") + "\n"
1123 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."
1124 ctx.ModuleErrorf(m)
1125 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001126 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001127 // Only add the deps for the library if it is actually going to be built.
1128 module.Library.deps(ctx)
1129 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001130}
1131
Paul Duffin46dc45a2020-05-14 15:39:10 +01001132func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1133 paths, err := module.commonOutputFiles(tag)
1134 if paths == nil && err == nil {
1135 return module.Library.OutputFiles(tag)
1136 } else {
1137 return paths, err
1138 }
1139}
1140
Inseob Kimc0907f12019-02-08 21:00:45 +09001141func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001142 module.generateCommonBuildActions(ctx)
1143
Paul Duffindfa131e2020-05-15 20:37:11 +01001144 // Only build an implementation library if required.
1145 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001146 module.Library.GenerateAndroidBuildActions(ctx)
1147 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001148
Sundong Ahn57368eb2018-07-06 11:20:23 +09001149 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001150 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001151 // the recorded paths will be returned depending on the link type of the caller.
1152 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001153 tag := ctx.OtherModuleDependencyTag(to)
1154
Paul Duffinc8782502020-04-29 20:45:27 +01001155 // Extract information from any of the scope specific dependencies.
1156 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1157 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001158 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001159
1160 // Extract information from the dependency. The exact information extracted
1161 // is determined by the nature of the dependency which is determined by the tag.
1162 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001163 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001164 })
1165}
1166
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001167func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001168 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001169 return nil
1170 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001171 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001172 if module.sharedLibrary() {
1173 entries := &entriesList[0]
1174 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1175 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001176 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001177}
1178
Anton Hansson5fd5d242020-03-27 19:43:19 +00001179// The dist path of the stub artifacts
1180func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1181 if module.ModuleBase.Owner() != "" {
1182 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1183 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1184 return path.Join("apistubs", "core", apiScope.name)
1185 } else {
1186 return path.Join("apistubs", "android", apiScope.name)
1187 }
1188}
1189
Paul Duffin12ceb462019-12-24 20:31:31 +00001190// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001191func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001192 scopeProperties := module.scopeToProperties[apiScope]
1193 if scopeProperties.Sdk_version != nil {
1194 return proptools.String(scopeProperties.Sdk_version)
1195 }
1196
Jiyong Parkf1691d22021-03-29 20:11:58 +09001197 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001198 if sdkDep.hasStandardLibs() {
1199 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001200 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001201 } else {
1202 // Otherwise, use no system module.
1203 return "none"
1204 }
1205}
1206
Paul Duffin31310252020-11-20 21:26:20 +00001207func (module *SdkLibrary) distStem() string {
1208 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1209}
1210
Paul Duffind1b3a922020-01-22 11:57:20 +00001211func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001212 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001213}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001214
Paul Duffind1b3a922020-01-22 11:57:20 +00001215func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001216 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001217}
1218
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001219func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
1220 return ":" + module.distStem() + "-incompatibilities.api." + apiScope.name + ".latest"
1221}
1222
Anton Hansson944e77d2020-08-19 11:40:22 +01001223func childModuleVisibility(childVisibility []string) []string {
1224 if childVisibility == nil {
1225 // No child visibility set. The child will use the visibility of the sdk_library.
1226 return nil
1227 }
1228
1229 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1230 var visibility []string
1231 visibility = append(visibility, "//visibility:override")
1232 visibility = append(visibility, childVisibility...)
1233 return visibility
1234}
1235
Paul Duffin5df79302020-05-16 15:52:12 +01001236// Creates the implementation java library
1237func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffina2058f82020-06-24 16:22:38 +01001238 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1239
Anton Hansson944e77d2020-08-19 11:40:22 +01001240 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1241
Paul Duffin5df79302020-05-16 15:52:12 +01001242 props := struct {
Paul Duffina2058f82020-06-24 16:22:38 +01001243 Name *string
1244 Visibility []string
1245 Instrument bool
Anton Hansson7f66efa2020-10-08 14:47:23 +01001246 Libs []string
Paul Duffina2058f82020-06-24 16:22:38 +01001247 ConfigurationName *string
Paul Duffin5df79302020-05-16 15:52:12 +01001248 }{
1249 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001250 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001251 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1252 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001253 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1254 // addition of &module.properties below.
1255 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffina2058f82020-06-24 16:22:38 +01001256
1257 // Make the created library behave as if it had the same name as this module.
1258 ConfigurationName: moduleNamePtr,
Paul Duffin5df79302020-05-16 15:52:12 +01001259 }
1260
1261 properties := []interface{}{
1262 &module.properties,
1263 &module.protoProperties,
1264 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001265 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001266 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001267 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001268 &props,
1269 module.sdkComponentPropertiesForChildLibrary(),
1270 }
1271 mctx.CreateModule(LibraryFactory, properties...)
1272}
1273
Jiyong Parkc678ad32018-04-10 13:07:10 +09001274// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001275func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001276 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001277 Name *string
1278 Visibility []string
1279 Srcs []string
1280 Installable *bool
1281 Sdk_version *string
1282 System_modules *string
1283 Patch_module *string
1284 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001285 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001286 Compile_dex *bool
1287 Java_version *string
1288 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001289 Srcs []string
1290 Javacflags []string
1291 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001292 Dist struct {
1293 Targets []string
1294 Dest *string
1295 Dir *string
1296 Tag *string
1297 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001298 }{}
1299
Paul Duffinc3091c82020-05-08 14:16:20 +01001300 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001301 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001302 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001303 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001304 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001305 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001306 props.System_modules = module.deviceProperties.System_modules
1307 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001308 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001309 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001310 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001311 // The stub-annotations library contains special versions of the annotations
1312 // with CLASS retention policy, so that they're kept.
1313 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1314 props.Libs = append(props.Libs, "stub-annotations")
1315 }
Paul Duffina18abc22020-05-16 18:54:24 +01001316 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1317 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001318 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1319 // interop with older developer tools that don't support 1.9.
1320 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001321
1322 // The imports need to be compiled to dex if the java_sdk_library requests it.
1323 compileDex := module.dexProperties.Compile_dex
1324 if module.stubLibrariesCompiledForDex() {
1325 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001326 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001327 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001328
Anton Hansson5fd5d242020-03-27 19:43:19 +00001329 // Dist the class jar artifact for sdk builds.
1330 if !Bool(module.sdkLibraryProperties.No_dist) {
1331 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001332 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001333 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1334 props.Dist.Tag = proptools.StringPtr(".jar")
1335 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001336
Paul Duffin859fe962020-05-15 10:20:31 +01001337 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001338}
1339
Paul Duffin6d0886e2020-04-07 18:49:53 +01001340// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001341// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001342func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001343 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001344 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001345 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001346 Srcs []string
1347 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001348 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001349 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001350 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001351 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001352 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001353 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001354 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001355 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001356 Merge_annotations_dirs []string
1357 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001358 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001359 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001360 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001361 Current ApiToCheck
1362 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001363
1364 Api_lint struct {
1365 Enabled *bool
1366 New_since *string
1367 Baseline_file *string
1368 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001369 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001370 Aidl struct {
1371 Include_dirs []string
1372 Local_include_dirs []string
1373 }
Paul Duffin040e9062020-11-23 17:41:36 +00001374 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001375 }{}
1376
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001377 // The stubs source processing uses the same compile time classpath when extracting the
1378 // API from the implementation library as it does when compiling it. i.e. the same
1379 // * sdk version
1380 // * system_modules
1381 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001382
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001383 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001384 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001385 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1386 props.Sdk_version = module.deviceProperties.Sdk_version
1387 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001388 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001389 // A droiddoc module has only one Libs property and doesn't distinguish between
1390 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001391 props.Libs = module.properties.Libs
1392 props.Libs = append(props.Libs, module.properties.Static_libs...)
1393 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1394 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1395 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001396
Paul Duffine22c2ab2020-05-20 19:35:27 +01001397 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001398 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1399 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1400
Paul Duffin6d0886e2020-04-07 18:49:53 +01001401 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001402 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001403 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001404 }
1405 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001406 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001407 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1408 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001409 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001410 disabledWarnings := []string{
1411 "MissingPermission",
1412 "BroadcastBehavior",
1413 "HiddenSuperclass",
1414 "DeprecationMismatch",
1415 "UnavailableSymbol",
1416 "SdkConstant",
1417 "HiddenTypeParameter",
1418 "Todo",
1419 "Typo",
1420 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001421 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001422
Paul Duffin6877e6d2020-09-25 19:59:14 +01001423 // Output Javadoc comments for public scope.
1424 if apiScope == apiScopePublic {
1425 props.Output_javadoc_comments = proptools.BoolPtr(true)
1426 }
1427
Paul Duffin1fb487d2020-04-07 18:50:10 +01001428 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001429 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001430 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001431 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001432
Paul Duffin15f34ef2020-07-20 18:04:44 +01001433 // List of APIs identified from the provided source files are created. They are later
1434 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1435 // last-released (a.k.a numbered) list of API.
1436 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1437 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1438 apiDir := module.getApiDir()
1439 currentApiFileName = path.Join(apiDir, currentApiFileName)
1440 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001441
Paul Duffin15f34ef2020-07-20 18:04:44 +01001442 // check against the not-yet-release API
1443 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1444 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001445
Anton Hanssone6056152020-12-31 10:37:27 +00001446 if !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001447 // check against the latest released API
1448 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001449 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001450 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1451 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1452 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001453 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1454 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001455
Paul Duffin15f34ef2020-07-20 18:04:44 +01001456 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1457 // Enable api lint.
1458 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1459 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001460
Paul Duffin15f34ef2020-07-20 18:04:44 +01001461 // If it exists then pass a lint-baseline.txt through to droidstubs.
1462 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1463 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1464 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1465 if err != nil {
1466 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1467 }
1468 if len(paths) == 1 {
1469 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1470 } else if len(paths) != 0 {
1471 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001472 }
1473 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001474 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001475
Paul Duffin15f34ef2020-07-20 18:04:44 +01001476 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001477 // Dist the api txt and removed api txt artifacts for sdk builds.
1478 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1479 for _, p := range []struct {
1480 tag string
1481 pattern string
1482 }{
1483 {tag: ".api.txt", pattern: "%s.txt"},
1484 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1485 } {
1486 props.Dists = append(props.Dists, android.Dist{
1487 Targets: []string{"sdk", "win_sdk"},
1488 Dir: distDir,
1489 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1490 Tag: proptools.StringPtr(p.tag),
1491 })
1492 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001493 }
1494
Colin Cross84dfc3d2019-09-25 11:33:01 -07001495 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001496}
1497
Jooyung Han5e9013b2020-03-10 06:23:13 +09001498func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1499 depTag := mctx.OtherModuleDependencyTag(dep)
1500 if depTag == xmlPermissionsFileTag {
1501 return true
1502 }
1503 return module.Library.DepIsInSameApex(mctx, dep)
1504}
1505
Jiyong Parkc678ad32018-04-10 13:07:10 +09001506// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001507func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001508 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001509 Name *string
1510 Lib_name *string
1511 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001512 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001513 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001514 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1515 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001516 }
Jiyong Parke3833882020-02-17 17:28:10 +09001517
Jiyong Parke3833882020-02-17 17:28:10 +09001518 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001519}
1520
Jiyong Parkf1691d22021-03-29 20:11:58 +09001521func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001522 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001523 var kind android.SdkKind
1524 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001525 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001526 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001527 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001528 // We don't have prebuilt SDK for the specific sdkVersion.
1529 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001530 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001531 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001532 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001533
1534 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001535 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001536 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001537 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001538 if ctx.Config().AllowMissingDependencies() {
1539 return android.Paths{android.PathForSource(ctx, jar)}
1540 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001541 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001542 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001543 return nil
1544 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001545 return android.Paths{jarPath.Path()}
1546}
1547
Colin Crossaede88c2020-08-11 12:17:01 -07001548// 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 +01001549//
1550// If either this or the other module are on the platform then this will return
1551// false.
Colin Cross56a83212020-09-15 18:30:11 -07001552func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1553 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1554 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
1555 return len(otherApexInfo.InApexes) > 0 && reflect.DeepEqual(apexInfo.InApexes, otherApexInfo.InApexes)
Paul Duffin9b879592020-05-26 13:21:35 +01001556}
1557
Jiyong Parkf1691d22021-03-29 20:11:58 +09001558func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001559 // If the client doesn't set sdk_version, but if this library prefers stubs over
1560 // the impl library, let's provide the widest API surface possible. To do so,
1561 // force override sdk_version to module_current so that the closest possible API
1562 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001563 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001564 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001565 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001566
Paul Duffindaaa3322020-05-26 18:13:57 +01001567 // Only provide access to the implementation library if it is actually built.
1568 if module.requiresRuntimeImplementationLibrary() {
1569 // Check any special cases for java_sdk_library.
1570 //
1571 // Only allow access to the implementation library in the following condition:
1572 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001573 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001574 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001575 if headerJars {
1576 return module.HeaderJars()
1577 } else {
1578 return module.ImplementationJars()
1579 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001580 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001581 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001582
Paul Duffin23970f42020-05-20 14:20:02 +01001583 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001584}
1585
Sundong Ahn241cd372018-07-13 16:16:44 +09001586// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001587func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001588 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1589}
1590
1591// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001592func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001593 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001594}
1595
Colin Cross571cccf2019-02-04 11:22:08 -08001596var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1597
Jiyong Park82484c02018-04-23 21:41:26 +09001598func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001599 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001600 return &[]string{}
1601 }).(*[]string)
1602}
1603
Paul Duffin749f98f2019-12-30 17:23:46 +00001604func (module *SdkLibrary) getApiDir() string {
1605 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1606}
1607
Jiyong Parkc678ad32018-04-10 13:07:10 +09001608// For a java_sdk_library module, create internal modules for stubs, docs,
1609// runtime libs and xml file. If requested, the stubs and docs are created twice
1610// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001611func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1612 // If the module has been disabled then don't create any child modules.
1613 if !module.Enabled() {
1614 return
1615 }
1616
Paul Duffina18abc22020-05-16 18:54:24 +01001617 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001618 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001619 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001620 }
1621
Paul Duffin37e0b772019-12-30 17:20:10 +00001622 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001623 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001624 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001625 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001626 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001627
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001628 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001629
Paul Duffin3375e352020-04-28 10:44:03 +01001630 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001631
Paul Duffin749f98f2019-12-30 17:23:46 +00001632 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001633 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001634 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001635 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001636 p := android.ExistentPathForSource(mctx, path)
1637 if !p.Valid() {
1638 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001639 missingCurrentApi = true
Inseob Kim8098faa2019-03-18 10:19:51 +09001640 }
1641 }
1642 }
1643
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001644 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001645 script := "build/soong/scripts/gen-java-current-api-files.sh"
1646 p := android.ExistentPathForSource(mctx, script)
1647
1648 if !p.Valid() {
1649 panic(fmt.Sprintf("script file %s doesn't exist", script))
1650 }
1651
1652 mctx.ModuleErrorf("One or more current api files are missing. "+
1653 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001654 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001655 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001656 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001657 return
1658 }
1659
Paul Duffin3375e352020-04-28 10:44:03 +01001660 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001661 // Use the stubs source name for legacy reasons.
1662 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001663
Paul Duffind1b3a922020-01-22 11:57:20 +00001664 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001665 }
1666
Paul Duffindfa131e2020-05-15 20:37:11 +01001667 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001668 // Create child module to create an implementation library.
1669 //
1670 // This temporarily creates a second implementation library that can be explicitly
1671 // referenced.
1672 //
1673 // TODO(b/156618935) - update comment once only one implementation library is created.
1674 module.createImplLibrary(mctx)
1675
Paul Duffindfa131e2020-05-15 20:37:11 +01001676 // Only create an XML permissions file that declares the library as being usable
1677 // as a shared library if required.
1678 if module.sharedLibrary() {
1679 module.createXmlFile(mctx)
1680 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001681
1682 // record java_sdk_library modules so that they are exported to make
1683 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1684 javaSdkLibrariesLock.Lock()
1685 defer javaSdkLibrariesLock.Unlock()
1686 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1687 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001688
1689 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1690 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001691}
1692
1693func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001694 module.addHostAndDeviceProperties()
1695 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001696
Paul Duffin859fe962020-05-15 10:20:31 +01001697 module.initSdkLibraryComponent(&module.ModuleBase)
1698
Paul Duffina18abc22020-05-16 18:54:24 +01001699 module.properties.Installable = proptools.BoolPtr(true)
1700 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001701}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001702
Paul Duffindfa131e2020-05-15 20:37:11 +01001703func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1704 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1705}
1706
Jiyong Park932cdfe2020-05-28 00:19:53 +09001707func (module *SdkLibrary) defaultsToStubs() bool {
1708 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1709}
1710
Paul Duffin1b1e8062020-05-08 13:44:43 +01001711// Defines how to name the individual component modules the sdk library creates.
1712type sdkLibraryComponentNamingScheme interface {
1713 stubsLibraryModuleName(scope *apiScope, baseName string) string
1714
1715 stubsSourceModuleName(scope *apiScope, baseName string) string
1716
1717 apiModuleName(scope *apiScope, baseName string) string
1718}
1719
1720type defaultNamingScheme struct {
1721}
1722
1723func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1724 return scope.stubsLibraryModuleName(baseName)
1725}
1726
1727func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1728 return scope.stubsSourceModuleName(baseName)
1729}
1730
1731func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1732 return scope.apiModuleName(baseName)
1733}
1734
1735var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1736
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001737func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001738 // This suffix-based approach is fragile and could potentially mis-trigger.
1739 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01001740 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
1741 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
1742 // Due to a previous bug, these modules were not considered stubs, so we retain that.
1743 return false, javaPlatform
1744 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001745 return true, javaSdk
1746 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001747 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001748 return true, javaSystem
1749 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001750 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001751 return true, javaModule
1752 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001753 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001754 return true, javaSystem
1755 }
1756 return false, javaPlatform
1757}
1758
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001759// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1760// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1761// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1762// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1763// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001764func SdkLibraryFactory() android.Module {
1765 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001766
1767 // Initialize information common between source and prebuilt.
1768 module.initCommon(&module.ModuleBase)
1769
Inseob Kimc0907f12019-02-08 21:00:45 +09001770 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001771 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +01001772 android.InitSdkAwareModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001773 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001774
1775 // Initialize the map from scope to scope specific properties.
1776 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1777 for _, scope := range allApiScopes {
1778 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1779 }
1780 module.scopeToProperties = scopeToProperties
1781
Paul Duffin4911a892020-04-29 23:35:13 +01001782 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001783 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001784 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1785 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1786
Paul Duffin1b1e8062020-05-08 13:44:43 +01001787 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001788 // If no implementation is required then it cannot be used as a shared library
1789 // either.
1790 if !module.requiresRuntimeImplementationLibrary() {
1791 // If shared_library has been explicitly set to true then it is incompatible
1792 // with api_only: true.
1793 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1794 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1795 }
1796 // Set shared_library: false.
1797 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1798 }
1799
Paul Duffin1b1e8062020-05-08 13:44:43 +01001800 if module.initCommonAfterDefaultsApplied(ctx) {
1801 module.CreateInternalModules(ctx)
1802 }
1803 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001804 return module
1805}
Colin Cross79c7c262019-04-17 11:11:46 -07001806
1807//
1808// SDK library prebuilts
1809//
1810
Paul Duffin56d44902020-01-31 13:36:25 +00001811// Properties associated with each api scope.
1812type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001813 Jars []string `android:"path"`
1814
1815 Sdk_version *string
1816
Colin Cross79c7c262019-04-17 11:11:46 -07001817 // List of shared java libs that this module has dependencies to
1818 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001819
Paul Duffinc8782502020-04-29 20:45:27 +01001820 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001821 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001822
1823 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001824 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001825
1826 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001827 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001828}
1829
Paul Duffin56d44902020-01-31 13:36:25 +00001830type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001831 // List of shared java libs, common to all scopes, that this module has
1832 // dependencies to
1833 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001834
1835 // If set to true, compile dex files for the stubs. Defaults to false.
1836 Compile_dex *bool
Paul Duffin56d44902020-01-31 13:36:25 +00001837}
1838
Paul Duffineedc5d52020-06-12 17:46:39 +01001839type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001840 android.ModuleBase
1841 android.DefaultableModuleBase
1842 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001843 android.ApexModuleBase
1844 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001845
Paul Duffin37856732021-02-26 14:24:15 +00001846 hiddenAPI
1847
Colin Cross79c7c262019-04-17 11:11:46 -07001848 properties sdkLibraryImportProperties
1849
Paul Duffin46a26a82020-04-07 19:27:04 +01001850 // Map from api scope to the scope specific property structure.
1851 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1852
Paul Duffin56d44902020-01-31 13:36:25 +00001853 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001854
1855 // The reference to the implementation library created by the source module.
1856 // Is nil if the source module does not exist.
1857 implLibraryModule *Library
1858
1859 // The reference to the xml permissions module created by the source module.
1860 // Is nil if the source module does not exist.
1861 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00001862
1863 // Path to the dex implementation jar obtained from the prebuilt_apex, if any.
1864 dexJarFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07001865}
1866
Paul Duffineedc5d52020-06-12 17:46:39 +01001867var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001868
Paul Duffin46a26a82020-04-07 19:27:04 +01001869// The type of a structure that contains a field of type sdkLibraryScopeProperties
1870// for each apiscope in allApiScopes, e.g. something like:
1871// struct {
1872// Public sdkLibraryScopeProperties
1873// System sdkLibraryScopeProperties
1874// ...
1875// }
1876var allScopeStructType = createAllScopePropertiesStructType()
1877
1878// Dynamically create a structure type for each apiscope in allApiScopes.
1879func createAllScopePropertiesStructType() reflect.Type {
1880 var fields []reflect.StructField
1881 for _, apiScope := range allApiScopes {
1882 field := reflect.StructField{
1883 Name: apiScope.fieldName,
1884 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1885 }
1886 fields = append(fields, field)
1887 }
1888
1889 return reflect.StructOf(fields)
1890}
1891
1892// Create an instance of the scope specific structure type and return a map
1893// from apiscope to a pointer to each scope specific field.
1894func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1895 allScopePropertiesPtr := reflect.New(allScopeStructType)
1896 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1897 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1898
1899 for _, apiScope := range allApiScopes {
1900 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1901 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1902 }
1903
1904 return allScopePropertiesPtr.Interface(), scopeProperties
1905}
1906
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001907// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001908func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001909 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001910
Paul Duffin46a26a82020-04-07 19:27:04 +01001911 allScopeProperties, scopeToProperties := createPropertiesInstance()
1912 module.scopeProperties = scopeToProperties
1913 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001914
Paul Duffinc3091c82020-05-08 14:16:20 +01001915 // Initialize information common between source and prebuilt.
1916 module.initCommon(&module.ModuleBase)
1917
Paul Duffin0bdcb272020-02-06 15:24:57 +00001918 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001919 android.InitApexModule(module)
1920 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001921 InitJavaModule(module, android.HostAndDeviceSupported)
1922
Paul Duffin1b1e8062020-05-08 13:44:43 +01001923 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1924 if module.initCommonAfterDefaultsApplied(mctx) {
1925 module.createInternalModules(mctx)
1926 }
1927 })
Colin Cross79c7c262019-04-17 11:11:46 -07001928 return module
1929}
1930
Paul Duffineedc5d52020-06-12 17:46:39 +01001931func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001932 return &module.prebuilt
1933}
1934
Paul Duffineedc5d52020-06-12 17:46:39 +01001935func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001936 return module.prebuilt.Name(module.ModuleBase.Name())
1937}
1938
Paul Duffineedc5d52020-06-12 17:46:39 +01001939func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001940
Paul Duffin50061512020-01-21 16:31:05 +00001941 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09001942 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00001943 module.prebuilt.ForcePrefer()
1944 }
1945
Paul Duffin46a26a82020-04-07 19:27:04 +01001946 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001947 if len(scopeProperties.Jars) == 0 {
1948 continue
1949 }
1950
Paul Duffinbbb546b2020-04-09 00:07:11 +01001951 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001952
Paul Duffin0f8faff2020-05-20 16:18:00 +01001953 if len(scopeProperties.Stub_srcs) > 0 {
1954 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1955 }
Paul Duffin56d44902020-01-31 13:36:25 +00001956 }
Colin Cross79c7c262019-04-17 11:11:46 -07001957
1958 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1959 javaSdkLibrariesLock.Lock()
1960 defer javaSdkLibrariesLock.Unlock()
1961 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1962}
1963
Paul Duffineedc5d52020-06-12 17:46:39 +01001964func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001965 // Creates a java import for the jar with ".stubs" suffix
1966 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001967 Name *string
1968 Sdk_version *string
1969 Libs []string
1970 Jars []string
1971 Prefer *bool
Paul Duffin1267d872021-04-16 17:21:36 +01001972 Compile_dex *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001973 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001974 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001975 props.Sdk_version = scopeProperties.Sdk_version
1976 // Prepend any of the libs from the legacy public properties to the libs for each of the
1977 // scopes to avoid having to duplicate them in each scope.
1978 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1979 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001980
Paul Duffin38b57852020-05-13 16:08:09 +01001981 // The imports are preferred if the java_sdk_library_import is preferred.
1982 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001983
Paul Duffin1267d872021-04-16 17:21:36 +01001984 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01001985 compileDex := module.properties.Compile_dex
1986 if module.stubLibrariesCompiledForDex() {
1987 compileDex = proptools.BoolPtr(true)
1988 }
1989 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01001990
Paul Duffin859fe962020-05-15 10:20:31 +01001991 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001992}
1993
Paul Duffineedc5d52020-06-12 17:46:39 +01001994func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001995 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001996 Name *string
1997 Srcs []string
1998 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001999 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002000 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002001 props.Srcs = scopeProperties.Stub_srcs
2002 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01002003
2004 // The stubs source is preferred if the java_sdk_library_import is preferred.
2005 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002006}
2007
Paul Duffin44f1d842020-06-26 20:17:02 +01002008// Add the dependencies on the child module in the component deps mutator so that it
2009// creates references to the prebuilt and not the source modules.
2010func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002011 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002012 if len(scopeProperties.Jars) == 0 {
2013 continue
2014 }
2015
2016 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002017 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002018
2019 if len(scopeProperties.Stub_srcs) > 0 {
2020 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002021 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002022 }
Paul Duffin56d44902020-01-31 13:36:25 +00002023 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002024}
2025
2026// Add other dependencies as normal.
2027func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002028
2029 implName := module.implLibraryModuleName()
2030 if ctx.OtherModuleExists(implName) {
2031 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2032
2033 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2034 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2035 // Add dependency to the rule for generating the xml permissions file
2036 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2037 }
2038 }
Colin Cross79c7c262019-04-17 11:11:46 -07002039}
2040
Jiyong Park45bf82e2020-12-15 22:29:02 +09002041var _ android.ApexModule = (*SdkLibraryImport)(nil)
2042
2043// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002044func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2045 depTag := mctx.OtherModuleDependencyTag(dep)
2046 if depTag == xmlPermissionsFileTag {
2047 return true
2048 }
2049
2050 // None of the other dependencies of the java_sdk_library_import are in the same apex
2051 // as the one that references this module.
2052 return false
2053}
2054
Jiyong Park45bf82e2020-12-15 22:29:02 +09002055// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002056func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2057 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002058 // we don't check prebuilt modules for sdk_version
2059 return nil
2060}
2061
Paul Duffineedc5d52020-06-12 17:46:39 +01002062func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01002063 return module.commonOutputFiles(tag)
2064}
2065
Paul Duffineedc5d52020-06-12 17:46:39 +01002066func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002067 module.generateCommonBuildActions(ctx)
2068
Paul Duffin39853512021-02-26 11:09:39 +00002069 var deapexerModule android.Module
2070
Paul Duffin0f8faff2020-05-20 16:18:00 +01002071 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002072 ctx.VisitDirectDeps(func(to android.Module) {
2073 tag := ctx.OtherModuleDependencyTag(to)
2074
Paul Duffin0f8faff2020-05-20 16:18:00 +01002075 // Extract information from any of the scope specific dependencies.
2076 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2077 apiScope := scopeTag.apiScope
2078 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2079
2080 // Extract information from the dependency. The exact information extracted
2081 // is determined by the nature of the dependency which is determined by the tag.
2082 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002083 } else if tag == implLibraryTag {
2084 if implLibrary, ok := to.(*Library); ok {
2085 module.implLibraryModule = implLibrary
2086 } else {
2087 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2088 }
2089 } else if tag == xmlPermissionsFileTag {
2090 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2091 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2092 } else {
2093 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2094 }
Colin Cross79c7c262019-04-17 11:11:46 -07002095 }
Paul Duffin39853512021-02-26 11:09:39 +00002096
2097 // Save away the `deapexer` module on which this depends, if any.
2098 if tag == android.DeapexerTag {
2099 deapexerModule = to
2100 }
Colin Cross79c7c262019-04-17 11:11:46 -07002101 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002102
2103 // Populate the scope paths with information from the properties.
2104 for apiScope, scopeProperties := range module.scopeProperties {
2105 if len(scopeProperties.Jars) == 0 {
2106 continue
2107 }
2108
2109 paths := module.getScopePathsCreateIfNeeded(apiScope)
2110 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2111 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2112 }
Paul Duffin39853512021-02-26 11:09:39 +00002113
2114 if ctx.Device() {
2115 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2116 // obtained from the associated deapexer module.
2117 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2118 if ai.ForPrebuiltApex {
2119 if deapexerModule == nil {
2120 // This should never happen as a variant for a prebuilt_apex is only created if the
2121 // deapxer module has been configured to export the dex implementation jar for this module.
2122 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
2123 module.Name(), ai.ApexVariationName)
2124 }
2125
2126 // Get the path of the dex implementation jar from the `deapexer` module.
2127 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
2128 if dexOutputPath := di.PrebuiltExportPath(module.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
2129 module.dexJarFile = dexOutputPath
Paul Duffin37856732021-02-26 14:24:15 +00002130 module.initHiddenAPI(ctx, module.configurationName)
2131 module.hiddenAPIExtractInformation(ctx, dexOutputPath, module.findScopePaths(apiScopePublic).stubsImplPath[0])
Paul Duffin39853512021-02-26 11:09:39 +00002132 } else {
2133 // This should never happen as a variant for a prebuilt_apex is only created if the
2134 // prebuilt_apex has been configured to export the java library dex file.
2135 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
2136 }
2137 }
2138 }
Colin Cross79c7c262019-04-17 11:11:46 -07002139}
2140
Jiyong Parkf1691d22021-03-29 20:11:58 +09002141func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002142
2143 // For consistency with SdkLibrary make the implementation jar available to libraries that
2144 // are within the same APEX.
2145 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002146 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002147 if headerJars {
2148 return implLibraryModule.HeaderJars()
2149 } else {
2150 return implLibraryModule.ImplementationJars()
2151 }
2152 }
2153
Paul Duffin23970f42020-05-20 14:20:02 +01002154 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002155}
2156
Colin Cross79c7c262019-04-17 11:11:46 -07002157// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002158func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002159 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002160 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002161}
2162
2163// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002164func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002165 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002166 return module.sdkJars(ctx, sdkVersion, false)
2167}
2168
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002169// to satisfy UsesLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002170func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
Paul Duffin39853512021-02-26 11:09:39 +00002171 // The dex implementation jar extracted from the .apex file should be used in preference to the
2172 // source.
2173 if module.dexJarFile != nil {
2174 return module.dexJarFile
2175 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002176 if module.implLibraryModule == nil {
2177 return nil
2178 } else {
2179 return module.implLibraryModule.DexJarBuildPath()
2180 }
2181}
2182
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002183// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002184func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
2185 if module.implLibraryModule == nil {
2186 return nil
2187 } else {
2188 return module.implLibraryModule.DexJarInstallPath()
2189 }
2190}
2191
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002192// to satisfy UsesLibraryDependency interface
2193func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2194 return nil
2195}
2196
Paul Duffineedc5d52020-06-12 17:46:39 +01002197// to satisfy apex.javaDependency interface
2198func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2199 if module.implLibraryModule == nil {
2200 return nil
2201 } else {
2202 return module.implLibraryModule.JacocoReportClassesFile()
2203 }
2204}
2205
2206// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002207func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2208 if module.implLibraryModule == nil {
2209 return LintDepSets{}
2210 } else {
2211 return module.implLibraryModule.LintDepSets()
2212 }
2213}
2214
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002215func (module *SdkLibraryImport) getStrictUpdatabilityLinting() bool {
2216 if module.implLibraryModule == nil {
2217 return false
2218 } else {
2219 return module.implLibraryModule.getStrictUpdatabilityLinting()
2220 }
2221}
2222
2223func (module *SdkLibraryImport) setStrictUpdatabilityLinting(strictLinting bool) {
2224 if module.implLibraryModule != nil {
2225 module.implLibraryModule.setStrictUpdatabilityLinting(strictLinting)
2226 }
2227}
2228
Colin Cross08dca382020-07-21 20:31:17 -07002229// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002230func (module *SdkLibraryImport) Stem() string {
2231 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002232}
Jiyong Parke3833882020-02-17 17:28:10 +09002233
Paul Duffin44b481b2020-06-17 16:59:43 +01002234var _ ApexDependency = (*SdkLibraryImport)(nil)
2235
2236// to satisfy java.ApexDependency interface
2237func (module *SdkLibraryImport) HeaderJars() android.Paths {
2238 if module.implLibraryModule == nil {
2239 return nil
2240 } else {
2241 return module.implLibraryModule.HeaderJars()
2242 }
2243}
2244
2245// to satisfy java.ApexDependency interface
2246func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2247 if module.implLibraryModule == nil {
2248 return nil
2249 } else {
2250 return module.implLibraryModule.ImplementationAndResourcesJars()
2251 }
2252}
2253
Jiyong Parke3833882020-02-17 17:28:10 +09002254//
2255// java_sdk_library_xml
2256//
2257type sdkLibraryXml struct {
2258 android.ModuleBase
2259 android.DefaultableModuleBase
2260 android.ApexModuleBase
2261
2262 properties sdkLibraryXmlProperties
2263
2264 outputFilePath android.OutputPath
2265 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002266
2267 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002268}
2269
2270type sdkLibraryXmlProperties struct {
2271 // canonical name of the lib
2272 Lib_name *string
2273}
2274
2275// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2276// Not to be used directly by users. java_sdk_library internally uses this.
2277func sdkLibraryXmlFactory() android.Module {
2278 module := &sdkLibraryXml{}
2279
2280 module.AddProperties(&module.properties)
2281
2282 android.InitApexModule(module)
2283 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2284
2285 return module
2286}
2287
Colin Crossaede88c2020-08-11 12:17:01 -07002288func (module *sdkLibraryXml) UniqueApexVariations() bool {
2289 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2290 // mounted APEX, which contains the name of the APEX.
2291 return true
2292}
2293
Jiyong Parke3833882020-02-17 17:28:10 +09002294// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002295func (module *sdkLibraryXml) BaseDir() string {
2296 return "etc"
2297}
2298
2299// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002300func (module *sdkLibraryXml) SubDir() string {
2301 return "permissions"
2302}
2303
2304// from android.PrebuiltEtcModule
2305func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2306 return module.outputFilePath
2307}
2308
2309// from android.ApexModule
2310func (module *sdkLibraryXml) AvailableFor(what string) bool {
2311 return true
2312}
2313
2314func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2315 // do nothing
2316}
2317
Jiyong Park45bf82e2020-12-15 22:29:02 +09002318var _ android.ApexModule = (*sdkLibraryXml)(nil)
2319
2320// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002321func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2322 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002323 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2324 return nil
2325}
2326
Jiyong Parke3833882020-02-17 17:28:10 +09002327// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002328func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002329 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002330 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002331 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002332 // In most cases, this works fine. But when apex_name is set or override_apex is used
2333 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002334 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002335 }
2336 partition := "system"
2337 if module.SocSpecific() {
2338 partition = "vendor"
2339 } else if module.DeviceSpecific() {
2340 partition = "odm"
2341 } else if module.ProductSpecific() {
2342 partition = "product"
2343 } else if module.SystemExtSpecific() {
2344 partition = "system_ext"
2345 }
2346 return "/" + partition + "/framework/" + implName + ".jar"
2347}
2348
2349func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002350 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2351
Jiyong Parke3833882020-02-17 17:28:10 +09002352 libName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002353 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath(ctx))
Jiyong Parke3833882020-02-17 17:28:10 +09002354
2355 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002356 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002357 rule.Command().
2358 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2359 Output(module.outputFilePath)
2360
Colin Crossf1a035e2020-11-16 17:32:30 -08002361 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002362
2363 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2364}
2365
2366func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002367 if module.hideApexVariantFromMake {
Jiyong Parke3833882020-02-17 17:28:10 +09002368 return []android.AndroidMkEntries{android.AndroidMkEntries{
2369 Disabled: true,
2370 }}
2371 }
2372
2373 return []android.AndroidMkEntries{android.AndroidMkEntries{
2374 Class: "ETC",
2375 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2376 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002377 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002378 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2379 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2380 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2381 },
2382 },
2383 }}
2384}
Paul Duffindd46f712020-02-10 13:37:10 +00002385
2386type sdkLibrarySdkMemberType struct {
2387 android.SdkMemberTypeBase
2388}
2389
2390func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2391 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2392}
2393
2394func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2395 _, ok := module.(*SdkLibrary)
2396 return ok
2397}
2398
2399func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2400 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2401}
2402
2403func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2404 return &sdkLibrarySdkMemberProperties{}
2405}
2406
Paul Duffin976b0e52021-04-27 23:20:26 +01002407var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2408 android.SdkMemberTypeBase{
2409 PropertyName: "java_sdk_libs",
2410 SupportsSdk: true,
2411 },
2412}
2413
Paul Duffindd46f712020-02-10 13:37:10 +00002414type sdkLibrarySdkMemberProperties struct {
2415 android.SdkMemberPropertiesBase
2416
2417 // Scope to per scope properties.
2418 Scopes map[*apiScope]scopeProperties
2419
Paul Duffin3d1248c2020-04-09 00:10:17 +01002420 // The Java stubs source files.
2421 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002422
2423 // The naming scheme.
2424 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002425
2426 // True if the java_sdk_library_import is for a shared library, false
2427 // otherwise.
2428 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002429
Paul Duffin1267d872021-04-16 17:21:36 +01002430 // True if the stub imports should produce dex jars.
2431 Compile_dex *bool
2432
Paul Duffina2ae7e02020-09-11 11:55:00 +01002433 // The paths to the doctag files to add to the prebuilt.
2434 Doctag_paths android.Paths
Paul Duffindd46f712020-02-10 13:37:10 +00002435}
2436
2437type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002438 Jars android.Paths
2439 StubsSrcJar android.Path
2440 CurrentApiFile android.Path
2441 RemovedApiFile android.Path
2442 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002443}
2444
2445func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2446 sdk := variant.(*SdkLibrary)
2447
2448 s.Scopes = make(map[*apiScope]scopeProperties)
2449 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002450 paths := sdk.findScopePaths(apiScope)
2451 if paths == nil {
2452 continue
2453 }
2454
Paul Duffindd46f712020-02-10 13:37:10 +00002455 jars := paths.stubsImplPath
2456 if len(jars) > 0 {
2457 properties := scopeProperties{}
2458 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002459 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002460 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002461 if paths.currentApiFilePath.Valid() {
2462 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2463 }
2464 if paths.removedApiFilePath.Valid() {
2465 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2466 }
Paul Duffindd46f712020-02-10 13:37:10 +00002467 s.Scopes[apiScope] = properties
2468 }
2469 }
2470
Paul Duffindfa131e2020-05-15 20:37:11 +01002471 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002472 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002473 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002474 s.Doctag_paths = sdk.doctagPaths
Paul Duffindd46f712020-02-10 13:37:10 +00002475}
2476
2477func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002478 if s.Naming_scheme != nil {
2479 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2480 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002481 if s.Shared_library != nil {
2482 propertySet.AddProperty("shared_library", *s.Shared_library)
2483 }
Paul Duffin1267d872021-04-16 17:21:36 +01002484 if s.Compile_dex != nil {
2485 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2486 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002487
Paul Duffindd46f712020-02-10 13:37:10 +00002488 for _, apiScope := range allApiScopes {
2489 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002490 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002491
Paul Duffin3d1248c2020-04-09 00:10:17 +01002492 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2493
Paul Duffindd46f712020-02-10 13:37:10 +00002494 var jars []string
2495 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002496 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002497 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2498 jars = append(jars, dest)
2499 }
2500 scopeSet.AddProperty("jars", jars)
2501
Paul Duffin22628d52021-05-12 23:13:22 +01002502 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
2503 // Copy the stubs source jar into the snapshot zip as is.
2504 srcJarSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".srcjar")
2505 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
2506 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
2507 } else {
2508 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2509 // the source files are also unpacked.
2510 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2511 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2512 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2513 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01002514
Paul Duffin1fd005d2020-04-09 01:08:11 +01002515 if properties.CurrentApiFile != nil {
2516 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2517 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2518 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2519 }
2520
2521 if properties.RemovedApiFile != nil {
2522 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002523 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002524 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2525 }
2526
Paul Duffindd46f712020-02-10 13:37:10 +00002527 if properties.SdkVersion != "" {
2528 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2529 }
2530 }
2531 }
2532
Paul Duffina2ae7e02020-09-11 11:55:00 +01002533 if len(s.Doctag_paths) > 0 {
2534 dests := []string{}
2535 for _, p := range s.Doctag_paths {
2536 dest := filepath.Join("doctags", p.Rel())
2537 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2538 dests = append(dests, dest)
2539 }
2540 propertySet.AddProperty("doctag_files", dests)
2541 }
Paul Duffindd46f712020-02-10 13:37:10 +00002542}