blob: 8f8f8ce636b8bd54dff7ee6bd571d49708ca2bf6 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090031)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Paul Duffindd9d0742020-05-08 15:52:37 +010034 sdkXmlFileSuffix = ".xml"
35 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090036 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090038 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 ` you may not use this file except in compliance with the License.\n` +
40 ` You may obtain a copy of the License at\n` +
41 `\n` +
42 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
43 `\n` +
44 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090045 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090046 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47 ` See the License for the specific language governing permissions and\n` +
48 ` limitations under the License.\n` +
49 `-->\n` +
50 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090051 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090052 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090053)
54
Paul Duffind1b3a922020-01-22 11:57:20 +000055// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57 blueprint.BaseDependencyTag
58 name string
59 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010060
61 // Function for extracting appropriate path information from the dependency.
62 depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67 err := tag.depInfoExtractor(paths, dep)
68 if err != nil {
69 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70 }
Paul Duffind1b3a922020-01-22 11:57:20 +000071}
72
73// Provides information about an api scope, e.g. public, system, test.
74type apiScope struct {
75 // The name of the api scope, e.g. public, system, test
76 name string
77
Paul Duffin97b53b82020-05-05 14:40:52 +010078 // The api scope that this scope extends.
79 extends *apiScope
80
Paul Duffin3375e352020-04-28 10:44:03 +010081 // The legacy enabled status for a specific scope can be dependent on other
82 // properties that have been specified on the library so it is provided by
83 // a function that can determine the status by examining those properties.
84 legacyEnabledStatus func(module *SdkLibrary) bool
85
86 // The default enabled status for non-legacy behavior, which is triggered by
87 // explicitly enabling at least one api scope.
88 defaultEnabledStatus bool
89
90 // Gets a pointer to the scope specific properties.
91 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
92
Paul Duffin46a26a82020-04-07 19:27:04 +010093 // The name of the field in the dynamically created structure.
94 fieldName string
95
Paul Duffin6b836ba2020-05-13 19:19:49 +010096 // The name of the property in the java_sdk_library_import
97 propertyName string
98
Paul Duffind1b3a922020-01-22 11:57:20 +000099 // The tag to use to depend on the stubs library module.
100 stubsTag scopeDependencyTag
101
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100102 // The tag to use to depend on the stubs source module (if separate from the API module).
103 stubsSourceTag scopeDependencyTag
104
105 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
106 apiFileTag scopeDependencyTag
107
Paul Duffinc8782502020-04-29 20:45:27 +0100108 // The tag to use to depend on the stubs source and API module.
109 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
111 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
112 apiFilePrefix string
113
114 // The scope specific prefix to add to the sdk library module name to construct a scope specific
115 // module name.
116 moduleSuffix string
117
Paul Duffind1b3a922020-01-22 11:57:20 +0000118 // SDK version that the stubs library is built against. Note that this is always
119 // *current. Older stubs library built with a numbered SDK version is created from
120 // the prebuilt jar.
121 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100122
123 // Extra arguments to pass to droidstubs for this scope.
124 droidstubsArgs []string
Anton Hansson6478ac12020-05-02 11:19:36 +0100125
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100126 // The args that must be passed to droidstubs to generate the stubs source
127 // for this scope.
128 //
129 // The stubs source must include the definitions of everything that is in this
130 // api scope and all the scopes that this one extends.
131 droidstubsArgsForGeneratingStubsSource []string
132
133 // The args that must be passed to droidstubs to generate the API for this scope.
134 //
135 // The API only includes the additional members that this scope adds over the scope
136 // that it extends.
137 droidstubsArgsForGeneratingApi []string
138
139 // True if the stubs source and api can be created by the same metalava invocation.
140 createStubsSourceAndApiTogether bool
141
Anton Hansson6478ac12020-05-02 11:19:36 +0100142 // Whether the api scope can be treated as unstable, and should skip compat checks.
143 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000144}
145
146// Initialize a scope, creating and adding appropriate dependency tags
147func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100148 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100149 scopeByName[name] = scope
150 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100151 scope.propertyName = strings.ReplaceAll(name, "-", "_")
152 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000153 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100154 name: name + "-stubs",
155 apiScope: scope,
156 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000157 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100158 scope.stubsSourceTag = scopeDependencyTag{
159 name: name + "-stubs-source",
160 apiScope: scope,
161 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
162 }
163 scope.apiFileTag = scopeDependencyTag{
164 name: name + "-api",
165 apiScope: scope,
166 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
167 }
Paul Duffinc8782502020-04-29 20:45:27 +0100168 scope.stubsSourceAndApiTag = scopeDependencyTag{
169 name: name + "-stubs-source-and-api",
170 apiScope: scope,
171 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000172 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100173
174 // To get the args needed to generate the stubs source append all the args from
175 // this scope and all the scopes it extends as each set of args adds additional
176 // members to the stubs.
177 var stubsSourceArgs []string
178 for s := scope; s != nil; s = s.extends {
179 stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
180 }
181 scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
182
183 // Currently the args needed to generate the API are the same as the args
184 // needed to add additional members.
185 apiArgs := scope.droidstubsArgs
186 scope.droidstubsArgsForGeneratingApi = apiArgs
187
188 // If the args needed to generate the stubs and API are the same then they
189 // can be generated in a single invocation of metalava, otherwise they will
190 // need separate invocations.
191 scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
192
Paul Duffind1b3a922020-01-22 11:57:20 +0000193 return scope
194}
195
Paul Duffinc3091c82020-05-08 14:16:20 +0100196func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100197 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000198}
199
Paul Duffinc8782502020-04-29 20:45:27 +0100200func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100201 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000202}
203
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100204func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100205 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100206}
207
Paul Duffin3375e352020-04-28 10:44:03 +0100208func (scope *apiScope) String() string {
209 return scope.name
210}
211
Paul Duffind1b3a922020-01-22 11:57:20 +0000212type apiScopes []*apiScope
213
214func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
215 var list []string
216 for _, scope := range scopes {
217 list = append(list, accessor(scope))
218 }
219 return list
220}
221
Jiyong Parkc678ad32018-04-10 13:07:10 +0900222var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100223 scopeByName = make(map[string]*apiScope)
224 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000225 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100226 name: "public",
227
228 // Public scope is enabled by default for both legacy and non-legacy modes.
229 legacyEnabledStatus: func(module *SdkLibrary) bool {
230 return true
231 },
232 defaultEnabledStatus: true,
233
234 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
235 return &module.sdkLibraryProperties.Public
236 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000237 sdkVersion: "current",
238 })
239 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100240 name: "system",
241 extends: apiScopePublic,
242 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
243 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
244 return &module.sdkLibraryProperties.System
245 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100246 apiFilePrefix: "system-",
Paul Duffindd9d0742020-05-08 15:52:37 +0100247 moduleSuffix: ".system",
Anton Hansson6affb1f2020-04-28 16:47:41 +0100248 sdkVersion: "system_current",
Paul Duffin0d543642020-04-29 22:18:41 +0100249 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000250 })
251 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100252 name: "test",
253 extends: apiScopePublic,
254 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
255 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
256 return &module.sdkLibraryProperties.Test
257 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100258 apiFilePrefix: "test-",
Paul Duffindd9d0742020-05-08 15:52:37 +0100259 moduleSuffix: ".test",
Anton Hansson6affb1f2020-04-28 16:47:41 +0100260 sdkVersion: "test_current",
261 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson6478ac12020-05-02 11:19:36 +0100262 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000263 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100264 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100265 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100266 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100267 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100268 //
269 // Enabling this would break existing usages.
270 legacyEnabledStatus: func(module *SdkLibrary) bool {
271 return false
272 },
273 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
274 return &module.sdkLibraryProperties.Module_lib
275 },
276 apiFilePrefix: "module-lib-",
277 moduleSuffix: ".module_lib",
278 sdkVersion: "module_current",
279 droidstubsArgs: []string{
280 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
281 },
282 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100283 apiScopeSystemServer = initApiScope(&apiScope{
284 name: "system-server",
285 extends: apiScopePublic,
286 // The system-server scope is disabled by default in legacy mode.
287 //
288 // Enabling this would break existing usages.
289 legacyEnabledStatus: func(module *SdkLibrary) bool {
290 return false
291 },
292 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
293 return &module.sdkLibraryProperties.System_server
294 },
295 apiFilePrefix: "system-server-",
296 moduleSuffix: ".system_server",
297 sdkVersion: "system_server_current",
298 droidstubsArgs: []string{
299 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.SYSTEM_SERVER\\) ",
300 "--hide-annotation android.annotation.Hide",
301 // com.android.* classes are okay in this interface"
302 "--hide InternalClasses",
303 },
304 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000305 allApiScopes = apiScopes{
306 apiScopePublic,
307 apiScopeSystem,
308 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100309 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100310 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000311 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900312)
313
Jiyong Park82484c02018-04-23 21:41:26 +0900314var (
315 javaSdkLibrariesLock sync.Mutex
316)
317
Jiyong Parkc678ad32018-04-10 13:07:10 +0900318// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900319// 1) disallowing linking to the runtime shared lib
320// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900321
322func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000323 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900324
Jiyong Park82484c02018-04-23 21:41:26 +0900325 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
326 javaSdkLibraries := javaSdkLibraries(ctx.Config())
327 sort.Strings(*javaSdkLibraries)
328 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
329 })
Paul Duffindd46f712020-02-10 13:37:10 +0000330
331 // Register sdk member types.
332 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
333 android.SdkMemberTypeBase{
334 PropertyName: "java_sdk_libs",
335 SupportsSdk: true,
336 },
337 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900338}
339
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000340func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
341 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
342 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
343}
344
Paul Duffin3375e352020-04-28 10:44:03 +0100345// Properties associated with each api scope.
346type ApiScopeProperties struct {
347 // Indicates whether the api surface is generated.
348 //
349 // If this is set for any scope then all scopes must explicitly specify if they
350 // are enabled. This is to prevent new usages from depending on legacy behavior.
351 //
352 // Otherwise, if this is not set for any scope then the default behavior is
353 // scope specific so please refer to the scope specific property documentation.
354 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100355
356 // The sdk_version to use for building the stubs.
357 //
358 // If not specified then it will use an sdk_version determined as follows:
359 // 1) If the sdk_version specified on the java_sdk_library is none then this
360 // will be none. This is used for java_sdk_library instances that are used
361 // to create stubs that contribute to the core_current sdk version.
362 // 2) Otherwise, it is assumed that this library extends but does not contribute
363 // directly to a specific sdk_version and so this uses the sdk_version appropriate
364 // for the api scope. e.g. public will use sdk_version: current, system will use
365 // sdk_version: system_current, etc.
366 //
367 // This does not affect the sdk_version used for either generating the stubs source
368 // or the API file. They both have to use the same sdk_version as is used for
369 // compiling the implementation library.
370 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100371}
372
Jiyong Parkc678ad32018-04-10 13:07:10 +0900373type sdkLibraryProperties struct {
Paul Duffin5df79302020-05-16 15:52:12 +0100374 // Visibility for impl library module. If not specified then defaults to the
375 // visibility property.
376 Impl_library_visibility []string
377
Paul Duffin4911a892020-04-29 23:35:13 +0100378 // Visibility for stubs library modules. If not specified then defaults to the
379 // visibility property.
380 Stubs_library_visibility []string
381
382 // Visibility for stubs source modules. If not specified then defaults to the
383 // visibility property.
384 Stubs_source_visibility []string
385
Sundong Ahnf043cf62018-06-25 16:04:37 +0900386 // List of Java libraries that will be in the classpath when building stubs
387 Stub_only_libs []string `android:"arch_variant"`
388
Paul Duffin7a586d32019-12-30 17:09:34 +0000389 // list of package names that will be documented and publicized as API.
390 // This allows the API to be restricted to a subset of the source files provided.
391 // If this is unspecified then all the source files will be treated as being part
392 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393 Api_packages []string
394
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900395 // list of package names that must be hidden from the API
396 Hidden_api_packages []string
397
Paul Duffin749f98f2019-12-30 17:23:46 +0000398 // the relative path to the directory containing the api specification files.
399 // Defaults to "api".
400 Api_dir *string
401
Paul Duffindfa131e2020-05-15 20:37:11 +0100402 // Determines whether a runtime implementation library is built; defaults to false.
403 //
404 // If true then it also prevents the module from being used as a shared module, i.e.
405 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000406 Api_only *bool
407
Paul Duffin11512472019-02-11 15:55:17 +0000408 // local files that are used within user customized droiddoc options.
409 Droiddoc_option_files []string
410
411 // additional droiddoc options
412 // Available variables for substitution:
413 //
414 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900415 Droiddoc_options []string
416
Sundong Ahn054b19a2018-10-19 13:46:09 +0900417 // a list of top-level directories containing files to merge qualifier annotations
418 // (i.e. those intended to be included in the stubs written) from.
419 Merge_annotations_dirs []string
420
421 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
422 Merge_inclusion_annotations_dirs []string
423
424 // If set to true, the path of dist files is apistubs/core. Defaults to false.
425 Core_lib *bool
426
Sundong Ahn80a87b32019-05-13 15:02:50 +0900427 // don't create dist rules.
428 No_dist *bool `blueprint:"mutated"`
429
Paul Duffin3375e352020-04-28 10:44:03 +0100430 // indicates whether system and test apis should be generated.
431 Generate_system_and_test_apis bool `blueprint:"mutated"`
432
433 // The properties specific to the public api scope
434 //
435 // Unless explicitly specified by using public.enabled the public api scope is
436 // enabled by default in both legacy and non-legacy mode.
437 Public ApiScopeProperties
438
439 // The properties specific to the system api scope
440 //
441 // In legacy mode the system api scope is enabled by default when sdk_version
442 // is set to something other than "none".
443 //
444 // In non-legacy mode the system api scope is disabled by default.
445 System ApiScopeProperties
446
447 // The properties specific to the test api scope
448 //
449 // In legacy mode the test api scope is enabled by default when sdk_version
450 // is set to something other than "none".
451 //
452 // In non-legacy mode the test api scope is disabled by default.
453 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000454
Paul Duffin0c5bae52020-06-02 13:00:08 +0100455 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100456 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100457 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100458 // disabled by default.
459 Module_lib ApiScopeProperties
460
Paul Duffin0c5bae52020-06-02 13:00:08 +0100461 // The properties specific to the system-server api scope
462 //
463 // Unless explicitly specified by using test.enabled the module-lib api scope is
464 // disabled by default.
465 System_server ApiScopeProperties
466
Jiyong Park932cdfe2020-05-28 00:19:53 +0900467 // Determines if the stubs are preferred over the implementation library
468 // for linking, even when the client doesn't specify sdk_version. When this
469 // is set to true, such clients are provided with the widest API surface that
470 // this lib provides. Note however that this option doesn't affect the clients
471 // that are in the same APEX as this library. In that case, the clients are
472 // always linked with the implementation library. Default is false.
473 Default_to_stubs *bool
474
Paul Duffin160fe412020-05-10 19:32:20 +0100475 // Properties related to api linting.
476 Api_lint struct {
477 // Enable api linting.
478 Enabled *bool
479 }
480
Jiyong Parkc678ad32018-04-10 13:07:10 +0900481 // TODO: determines whether to create HTML doc or not
482 //Html_doc *bool
483}
484
Paul Duffin0f8faff2020-05-20 16:18:00 +0100485// Paths to outputs from java_sdk_library and java_sdk_library_import.
486//
487// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
488// OptionalPaths are always set by java_sdk_library but may not be set by
489// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000490type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100491 // The path (represented as Paths for convenience when returning) to the stubs header jar.
492 //
493 // That is the jar that is created by turbine.
494 stubsHeaderPath android.Paths
495
496 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
497 //
498 // This is not the implementation jar, it still only contains stubs.
499 stubsImplPath android.Paths
500
501 // The API specification file, e.g. system_current.txt.
502 currentApiFilePath android.OptionalPath
503
504 // The specification of API elements removed since the last release.
505 removedApiFilePath android.OptionalPath
506
507 // The stubs source jar.
508 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000509}
510
Paul Duffinc8782502020-04-29 20:45:27 +0100511func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
512 if lib, ok := dep.(Dependency); ok {
513 paths.stubsHeaderPath = lib.HeaderJars()
514 paths.stubsImplPath = lib.ImplementationJars()
515 return nil
516 } else {
517 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
518 }
519}
520
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100521func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
522 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
523 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100524 return nil
525 } else {
526 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
527 }
528}
529
Paul Duffin0f8faff2020-05-20 16:18:00 +0100530func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
531 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
532 action(apiStubsProvider)
533 return nil
534 } else {
535 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
536 }
537}
538
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100539func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100540 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
541 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100542}
543
544func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
545 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
546 paths.extractApiInfoFromApiStubsProvider(provider)
547 })
548}
549
Paul Duffin0f8faff2020-05-20 16:18:00 +0100550func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
551 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100552}
553
554func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100555 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100556 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
557 })
558}
559
560func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
561 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
562 paths.extractApiInfoFromApiStubsProvider(provider)
563 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
564 })
565}
566
567type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100568 // The naming scheme to use for the components that this module creates.
569 //
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100570 // If not specified then it defaults to "default". The other allowable value is
571 // "framework-modules" which matches the scheme currently used by framework modules
572 // for the equivalent components represented as separate Soong modules.
Paul Duffin1b1e8062020-05-08 13:44:43 +0100573 //
574 // This is a temporary mechanism to simplify conversion from separate modules for each
575 // component that follow a different naming pattern to the default one.
576 //
577 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100578 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100579
580 // Specifies whether this module can be used as an Android shared library; defaults
581 // to true.
582 //
583 // An Android shared library is one that can be referenced in a <uses-library> element
584 // in an AndroidManifest.xml.
585 Shared_library *bool
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100586}
587
Paul Duffin56d44902020-01-31 13:36:25 +0000588// Common code between sdk library and sdk library import
589type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100590 moduleBase *android.ModuleBase
591
Paul Duffin56d44902020-01-31 13:36:25 +0000592 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100593
594 namingScheme sdkLibraryComponentNamingScheme
595
Paul Duffindfa131e2020-05-15 20:37:11 +0100596 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100597
598 // Functionality related to this being used as a component of a java_sdk_library.
599 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000600}
601
Paul Duffinc3091c82020-05-08 14:16:20 +0100602func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
603 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100604
Paul Duffindfa131e2020-05-15 20:37:11 +0100605 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100606
607 // Initialize this as an sdk library component.
608 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100609}
610
611func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100612 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100613 switch schemeProperty {
614 case "default":
615 c.namingScheme = &defaultNamingScheme{}
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100616 case "framework-modules":
617 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1b1e8062020-05-08 13:44:43 +0100618 default:
619 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
620 return false
621 }
622
Paul Duffindfa131e2020-05-15 20:37:11 +0100623 // Only track this sdk library if this can be used as a shared library.
624 if c.sharedLibrary() {
625 // Use the name specified in the module definition as the owner.
626 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
627 }
Paul Duffin859fe962020-05-15 10:20:31 +0100628
Paul Duffin1b1e8062020-05-08 13:44:43 +0100629 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100630}
631
Paul Duffineedc5d52020-06-12 17:46:39 +0100632// Module name of the runtime implementation library
633func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
634 return c.moduleBase.BaseModuleName() + ".impl"
635}
636
637// Module name of the XML file for the lib
638func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
639 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
640}
641
Paul Duffinc3091c82020-05-08 14:16:20 +0100642// Name of the java_library module that compiles the stubs source.
643func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100644 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100645}
646
647// Name of the droidstubs module that generates the stubs source and may also
648// generate/check the API.
649func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100650 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100651}
652
653// Name of the droidstubs module that generates/checks the API. Only used if it
654// requires different arts to the stubs source generating module.
655func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100656 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100657}
658
Paul Duffin46dc45a2020-05-14 15:39:10 +0100659// The component names for different outputs of the java_sdk_library.
660//
661// They are similar to the names used for the child modules it creates
662const (
663 stubsSourceComponentName = "stubs.source"
664
665 apiTxtComponentName = "api.txt"
666
667 removedApiTxtComponentName = "removed-api.txt"
668)
669
670// A regular expression to match tags that reference a specific stubs component.
671//
672// It will only match if given a valid scope and a valid component. It is verfy strict
673// to ensure it does not accidentally match a similar looking tag that should be processed
674// by the embedded Library.
675var tagSplitter = func() *regexp.Regexp {
676 // Given a list of literal string items returns a regular expression that will
677 // match any one of the items.
678 choice := func(items ...string) string {
679 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
680 }
681
682 // Regular expression to match one of the scopes.
683 scopesRegexp := choice(allScopeNames...)
684
685 // Regular expression to match one of the components.
686 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
687
688 // Regular expression to match any combination of one scope and one component.
689 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
690}()
691
692// For OutputFileProducer interface
693//
694// .<scope>.stubs.source
695// .<scope>.api.txt
696// .<scope>.removed-api.txt
697func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
698 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
699 scopeName := groups[1]
700 component := groups[2]
701
702 if scope, ok := scopeByName[scopeName]; ok {
703 paths := c.findScopePaths(scope)
704 if paths == nil {
705 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
706 }
707
708 switch component {
709 case stubsSourceComponentName:
710 if paths.stubsSrcJar.Valid() {
711 return android.Paths{paths.stubsSrcJar.Path()}, nil
712 }
713
714 case apiTxtComponentName:
715 if paths.currentApiFilePath.Valid() {
716 return android.Paths{paths.currentApiFilePath.Path()}, nil
717 }
718
719 case removedApiTxtComponentName:
720 if paths.removedApiFilePath.Valid() {
721 return android.Paths{paths.removedApiFilePath.Path()}, nil
722 }
723 }
724
725 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
726 } else {
727 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
728 }
729
730 } else {
731 return nil, nil
732 }
733}
734
Paul Duffin803a9562020-05-20 11:52:25 +0100735func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000736 if c.scopePaths == nil {
737 c.scopePaths = make(map[*apiScope]*scopePaths)
738 }
739 paths := c.scopePaths[scope]
740 if paths == nil {
741 paths = &scopePaths{}
742 c.scopePaths[scope] = paths
743 }
744
745 return paths
746}
747
Paul Duffin803a9562020-05-20 11:52:25 +0100748func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
749 if c.scopePaths == nil {
750 return nil
751 }
752
753 return c.scopePaths[scope]
754}
755
756// If this does not support the requested api scope then find the closest available
757// scope it does support. Returns nil if no such scope is available.
758func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
759 for s := scope; s != nil; s = s.extends {
760 if paths := c.findScopePaths(s); paths != nil {
761 return paths
762 }
763 }
764
765 // This should never happen outside tests as public should be the base scope for every
766 // scope and is enabled by default.
767 return nil
768}
769
Paul Duffin23970f42020-05-20 14:20:02 +0100770func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100771
772 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
773 if sdkVersion.version.isNumbered() {
774 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
775 }
776
777 var apiScope *apiScope
778 switch sdkVersion.kind {
779 case sdkSystem:
780 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100781 case sdkModule:
782 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100783 case sdkTest:
784 apiScope = apiScopeTest
Paul Duffin0c5bae52020-06-02 13:00:08 +0100785 case sdkSystemServer:
786 apiScope = apiScopeSystemServer
Paul Duffinb05d4292020-05-20 12:19:10 +0100787 default:
788 apiScope = apiScopePublic
789 }
790
Paul Duffin803a9562020-05-20 11:52:25 +0100791 paths := c.findClosestScopePath(apiScope)
792 if paths == nil {
793 var scopes []string
794 for _, s := range allApiScopes {
795 if c.findScopePaths(s) != nil {
796 scopes = append(scopes, s.name)
797 }
798 }
799 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
800 return nil
801 }
802
Paul Duffin23970f42020-05-20 14:20:02 +0100803 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100804}
805
Paul Duffin859fe962020-05-15 10:20:31 +0100806func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
807 componentProps := &struct {
808 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100809 }{}
810
811 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100812 // Mark the stubs library as being components of this java_sdk_library so that
813 // any app that includes code which depends (directly or indirectly) on the stubs
814 // library will have the appropriate <uses-library> invocation inserted into its
815 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100816 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100817 }
818
819 return componentProps
820}
821
Paul Duffindfa131e2020-05-15 20:37:11 +0100822// Check if this can be used as a shared library.
823func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
824 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
825}
826
Paul Duffin859fe962020-05-15 10:20:31 +0100827// Properties related to the use of a module as an component of a java_sdk_library.
828type SdkLibraryComponentProperties struct {
829
830 // The name of the java_sdk_library/_import to add to a <uses-library> entry
831 // in the AndroidManifest.xml of any Android app that includes code that references
832 // this module. If not set then no java_sdk_library/_import is tracked.
833 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
834}
835
836// Structure to be embedded in a module struct that needs to support the
837// SdkLibraryComponentDependency interface.
838type EmbeddableSdkLibraryComponent struct {
839 sdkLibraryComponentProperties SdkLibraryComponentProperties
840}
841
842func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
843 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
844}
845
846// to satisfy SdkLibraryComponentDependency
847func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
848 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
849 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
850 }
851 return nil
852}
853
854// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
855// (including the java_sdk_library) itself.
856type SdkLibraryComponentDependency interface {
857 // The optional name of the sdk library that should be implicitly added to the
858 // AndroidManifest of an app that contains code which references the sdk library.
859 //
860 // Returns an array containing 0 or 1 items rather than a *string to make it easier
861 // to append this to the list of exported sdk libraries.
862 OptionalImplicitSdkLibrary() []string
863}
864
865// Make sure that all the module types that are components of java_sdk_library/_import
866// and which can be referenced (directly or indirectly) from an android app implement
867// the SdkLibraryComponentDependency interface.
868var _ SdkLibraryComponentDependency = (*Library)(nil)
869var _ SdkLibraryComponentDependency = (*Import)(nil)
870var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100871var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +0100872
873// Provides access to sdk_version related header and implentation jars.
874type SdkLibraryDependency interface {
875 SdkLibraryComponentDependency
876
877 // Get the header jars appropriate for the supplied sdk_version.
878 //
879 // These are turbine generated jars so they only change if the externals of the
880 // class changes but it does not contain and implementation or JavaDoc.
881 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
882
883 // Get the implementation jars appropriate for the supplied sdk version.
884 //
885 // These are either the implementation jar for the whole sdk library or the implementation
886 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
887 // they are identical to the corresponding header jars.
888 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
889}
890
Inseob Kimc0907f12019-02-08 21:00:45 +0900891type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900892 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900893
Sundong Ahn054b19a2018-10-19 13:46:09 +0900894 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900895
Paul Duffin3375e352020-04-28 10:44:03 +0100896 // Map from api scope to the scope specific property structure.
897 scopeToProperties map[*apiScope]*ApiScopeProperties
898
Paul Duffin56d44902020-01-31 13:36:25 +0000899 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900900}
901
Inseob Kimc0907f12019-02-08 21:00:45 +0900902var _ Dependency = (*SdkLibrary)(nil)
903var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800904
Paul Duffin3375e352020-04-28 10:44:03 +0100905func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
906 return module.sdkLibraryProperties.Generate_system_and_test_apis
907}
908
909func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
910 // Check to see if any scopes have been explicitly enabled. If any have then all
911 // must be.
912 anyScopesExplicitlyEnabled := false
913 for _, scope := range allApiScopes {
914 scopeProperties := module.scopeToProperties[scope]
915 if scopeProperties.Enabled != nil {
916 anyScopesExplicitlyEnabled = true
917 break
918 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000919 }
Paul Duffin3375e352020-04-28 10:44:03 +0100920
921 var generatedScopes apiScopes
922 enabledScopes := make(map[*apiScope]struct{})
923 for _, scope := range allApiScopes {
924 scopeProperties := module.scopeToProperties[scope]
925 // If any scopes are explicitly enabled then ignore the legacy enabled status.
926 // This is to ensure that any new usages of this module type do not rely on legacy
927 // behaviour.
928 defaultEnabledStatus := false
929 if anyScopesExplicitlyEnabled {
930 defaultEnabledStatus = scope.defaultEnabledStatus
931 } else {
932 defaultEnabledStatus = scope.legacyEnabledStatus(module)
933 }
934 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
935 if enabled {
936 enabledScopes[scope] = struct{}{}
937 generatedScopes = append(generatedScopes, scope)
938 }
939 }
940
941 // Now check to make sure that any scope that is extended by an enabled scope is also
942 // enabled.
943 for _, scope := range allApiScopes {
944 if _, ok := enabledScopes[scope]; ok {
945 extends := scope.extends
946 if extends != nil {
947 if _, ok := enabledScopes[extends]; !ok {
948 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
949 }
950 }
951 }
952 }
953
954 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000955}
956
Paul Duffineedc5d52020-06-12 17:46:39 +0100957type sdkLibraryComponentTag struct {
958 blueprint.BaseDependencyTag
959 name string
960}
961
962// Mark this tag so dependencies that use it are excluded from visibility enforcement.
963func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
964
965var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000966
Jiyong Parke3833882020-02-17 17:28:10 +0900967func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +0100968 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900969 return dt == xmlPermissionsFileTag
970 }
971 return false
972}
973
Paul Duffineedc5d52020-06-12 17:46:39 +0100974var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +0100975
Inseob Kimc0907f12019-02-08 21:00:45 +0900976func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100977 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000978 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100979 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000980
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100981 // If the stubs source and API cannot be generated together then add an additional dependency on
982 // the API module.
983 if apiScope.createStubsSourceAndApiTogether {
984 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinc3091c82020-05-08 14:16:20 +0100985 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100986 } else {
987 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinc3091c82020-05-08 14:16:20 +0100988 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
989 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100990 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900991 }
992
Paul Duffindfa131e2020-05-15 20:37:11 +0100993 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +0100994 // Add dependency to the rule for generating the implementation library.
995 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
996
Paul Duffindfa131e2020-05-15 20:37:11 +0100997 if module.sharedLibrary() {
998 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +0100999 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001000 }
Paul Duffine74ac732020-02-06 13:51:46 +00001001
Paul Duffindfa131e2020-05-15 20:37:11 +01001002 // Only add the deps for the library if it is actually going to be built.
1003 module.Library.deps(ctx)
1004 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001005}
1006
Paul Duffin46dc45a2020-05-14 15:39:10 +01001007func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1008 paths, err := module.commonOutputFiles(tag)
1009 if paths == nil && err == nil {
1010 return module.Library.OutputFiles(tag)
1011 } else {
1012 return paths, err
1013 }
1014}
1015
Inseob Kimc0907f12019-02-08 21:00:45 +09001016func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001017 // Only build an implementation library if required.
1018 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001019 module.Library.GenerateAndroidBuildActions(ctx)
1020 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001021
Sundong Ahn57368eb2018-07-06 11:20:23 +09001022 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001023 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001024 // the recorded paths will be returned depending on the link type of the caller.
1025 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001026 tag := ctx.OtherModuleDependencyTag(to)
1027
Paul Duffinc8782502020-04-29 20:45:27 +01001028 // Extract information from any of the scope specific dependencies.
1029 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1030 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001031 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001032
1033 // Extract information from the dependency. The exact information extracted
1034 // is determined by the nature of the dependency which is determined by the tag.
1035 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001036 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001037 })
1038}
1039
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001040func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001041 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001042 return nil
1043 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001044 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001045 if module.sharedLibrary() {
1046 entries := &entriesList[0]
1047 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1048 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001049 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001050}
1051
Anton Hansson5fd5d242020-03-27 19:43:19 +00001052// The dist path of the stub artifacts
1053func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1054 if module.ModuleBase.Owner() != "" {
1055 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1056 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1057 return path.Join("apistubs", "core", apiScope.name)
1058 } else {
1059 return path.Join("apistubs", "android", apiScope.name)
1060 }
1061}
1062
Paul Duffin12ceb462019-12-24 20:31:31 +00001063// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001064func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001065 scopeProperties := module.scopeToProperties[apiScope]
1066 if scopeProperties.Sdk_version != nil {
1067 return proptools.String(scopeProperties.Sdk_version)
1068 }
1069
Paul Duffin12ceb462019-12-24 20:31:31 +00001070 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1071 if sdkDep.hasStandardLibs() {
1072 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001073 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001074 } else {
1075 // Otherwise, use no system module.
1076 return "none"
1077 }
1078}
1079
Paul Duffind1b3a922020-01-22 11:57:20 +00001080func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1081 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001082}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001083
Paul Duffind1b3a922020-01-22 11:57:20 +00001084func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1085 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001086}
1087
Paul Duffin5df79302020-05-16 15:52:12 +01001088// Creates the implementation java library
1089func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffina2058f82020-06-24 16:22:38 +01001090
1091 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1092
Paul Duffin5df79302020-05-16 15:52:12 +01001093 props := struct {
Paul Duffina2058f82020-06-24 16:22:38 +01001094 Name *string
1095 Visibility []string
1096 Instrument bool
1097 ConfigurationName *string
Paul Duffin5df79302020-05-16 15:52:12 +01001098 }{
1099 Name: proptools.StringPtr(module.implLibraryModuleName()),
1100 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001101 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1102 Instrument: true,
Paul Duffina2058f82020-06-24 16:22:38 +01001103
1104 // Make the created library behave as if it had the same name as this module.
1105 ConfigurationName: moduleNamePtr,
Paul Duffin5df79302020-05-16 15:52:12 +01001106 }
1107
1108 properties := []interface{}{
1109 &module.properties,
1110 &module.protoProperties,
1111 &module.deviceProperties,
1112 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001113 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001114 &props,
1115 module.sdkComponentPropertiesForChildLibrary(),
1116 }
1117 mctx.CreateModule(LibraryFactory, properties...)
1118}
1119
Jiyong Parkc678ad32018-04-10 13:07:10 +09001120// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001121func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001122 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001123 Name *string
1124 Visibility []string
1125 Srcs []string
1126 Installable *bool
1127 Sdk_version *string
1128 System_modules *string
1129 Patch_module *string
1130 Libs []string
1131 Compile_dex *bool
1132 Java_version *string
1133 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001134 Pdk struct {
1135 Enabled *bool
1136 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001137 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001138 Openjdk9 struct {
1139 Srcs []string
1140 Javacflags []string
1141 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001142 Dist struct {
1143 Targets []string
1144 Dest *string
1145 Dir *string
1146 Tag *string
1147 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001148 }{}
1149
Paul Duffinc3091c82020-05-08 14:16:20 +01001150 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +01001151
1152 // If stubs_library_visibility is not set then the created module will use the
1153 // visibility of this module.
1154 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1155 props.Visibility = visibility
1156
Jiyong Parkc678ad32018-04-10 13:07:10 +09001157 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001158 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001159 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001160 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001161 props.System_modules = module.deviceProperties.System_modules
1162 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001163 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001164 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +09001165 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffina18abc22020-05-16 18:54:24 +01001166 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1167 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001168 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1169 // interop with older developer tools that don't support 1.9.
1170 props.Java_version = proptools.StringPtr("1.8")
Paul Duffina18abc22020-05-16 18:54:24 +01001171 if module.deviceProperties.Compile_dex != nil {
1172 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001173 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001174
Anton Hansson5fd5d242020-03-27 19:43:19 +00001175 // Dist the class jar artifact for sdk builds.
1176 if !Bool(module.sdkLibraryProperties.No_dist) {
1177 props.Dist.Targets = []string{"sdk", "win_sdk"}
1178 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1179 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1180 props.Dist.Tag = proptools.StringPtr(".jar")
1181 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001182
Paul Duffin859fe962020-05-15 10:20:31 +01001183 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001184}
1185
Paul Duffin6d0886e2020-04-07 18:49:53 +01001186// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001187// files and also updates and checks the API specification files.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001188func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001189 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001190 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001191 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001192 Srcs []string
1193 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001194 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001195 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001196 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001197 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001198 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001199 Java_version *string
1200 Merge_annotations_dirs []string
1201 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001202 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001203 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001204 Current ApiToCheck
1205 Last_released ApiToCheck
1206 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001207
1208 Api_lint struct {
1209 Enabled *bool
1210 New_since *string
1211 Baseline_file *string
1212 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001213 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001214 Aidl struct {
1215 Include_dirs []string
1216 Local_include_dirs []string
1217 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001218 Dist struct {
1219 Targets []string
1220 Dest *string
1221 Dir *string
1222 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001223 }{}
1224
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001225 // The stubs source processing uses the same compile time classpath when extracting the
1226 // API from the implementation library as it does when compiling it. i.e. the same
1227 // * sdk version
1228 // * system_modules
1229 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001230
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001231 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +01001232
1233 // If stubs_source_visibility is not set then the created module will use the
1234 // visibility of this module.
1235 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1236 props.Visibility = visibility
1237
Paul Duffina18abc22020-05-16 18:54:24 +01001238 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1239 props.Sdk_version = module.deviceProperties.Sdk_version
1240 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001241 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001242 // A droiddoc module has only one Libs property and doesn't distinguish between
1243 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001244 props.Libs = module.properties.Libs
1245 props.Libs = append(props.Libs, module.properties.Static_libs...)
1246 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1247 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1248 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001249
Sundong Ahn054b19a2018-10-19 13:46:09 +09001250 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1251 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1252
Paul Duffin6d0886e2020-04-07 18:49:53 +01001253 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001254 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001255 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001256 }
1257 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001258 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001259 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1260 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001261 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001262 disabledWarnings := []string{
1263 "MissingPermission",
1264 "BroadcastBehavior",
1265 "HiddenSuperclass",
1266 "DeprecationMismatch",
1267 "UnavailableSymbol",
1268 "SdkConstant",
1269 "HiddenTypeParameter",
1270 "Todo",
1271 "Typo",
1272 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001273 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001274
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001275 if !createStubSources {
1276 // Stubs are not required.
1277 props.Generate_stubs = proptools.BoolPtr(false)
1278 }
1279
Paul Duffin1fb487d2020-04-07 18:50:10 +01001280 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001281 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001282 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001283 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001284
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001285 if createApi {
1286 // List of APIs identified from the provided source files are created. They are later
1287 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1288 // last-released (a.k.a numbered) list of API.
1289 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1290 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1291 apiDir := module.getApiDir()
1292 currentApiFileName = path.Join(apiDir, currentApiFileName)
1293 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001294
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001295 // check against the not-yet-release API
1296 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1297 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001298
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001299 if !apiScope.unstable {
1300 // check against the latest released API
1301 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1302 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1303 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1304 module.latestRemovedApiFilegroupName(apiScope))
1305 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001306
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001307 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1308 // Enable api lint.
1309 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1310 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001311
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001312 // If it exists then pass a lint-baseline.txt through to droidstubs.
1313 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1314 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1315 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1316 if err != nil {
1317 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1318 }
1319 if len(paths) == 1 {
1320 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1321 } else if len(paths) != 0 {
1322 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1323 }
Paul Duffin160fe412020-05-10 19:32:20 +01001324 }
1325 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001326
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001327 // Dist the api txt artifact for sdk builds.
1328 if !Bool(module.sdkLibraryProperties.No_dist) {
1329 props.Dist.Targets = []string{"sdk", "win_sdk"}
1330 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1331 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1332 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001333 }
1334
Colin Cross84dfc3d2019-09-25 11:33:01 -07001335 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001336}
1337
Jooyung Han5e9013b2020-03-10 06:23:13 +09001338func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1339 depTag := mctx.OtherModuleDependencyTag(dep)
1340 if depTag == xmlPermissionsFileTag {
1341 return true
1342 }
1343 return module.Library.DepIsInSameApex(mctx, dep)
1344}
1345
Jiyong Parkc678ad32018-04-10 13:07:10 +09001346// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001347func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001348 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001349 Name *string
1350 Lib_name *string
1351 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001352 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001353 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001354 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1355 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001356 }
Jiyong Parke3833882020-02-17 17:28:10 +09001357
Jiyong Parke3833882020-02-17 17:28:10 +09001358 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001359}
1360
Paul Duffin50061512020-01-21 16:31:05 +00001361func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001362 var ver sdkVersion
1363 var kind sdkKind
1364 if s.usePrebuilt(ctx) {
1365 ver = s.version
1366 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001367 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001368 // We don't have prebuilt SDK for the specific sdkVersion.
1369 // Instead of breaking the build, fallback to use "system_current"
1370 ver = sdkVersionCurrent
1371 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001372 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001373
1374 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001375 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001376 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001377 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001378 if ctx.Config().AllowMissingDependencies() {
1379 return android.Paths{android.PathForSource(ctx, jar)}
1380 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001381 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001382 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001383 return nil
1384 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001385 return android.Paths{jarPath.Path()}
1386}
1387
Paul Duffin9b879592020-05-26 13:21:35 +01001388// Get the apex name for module, "" if it is for platform.
1389func getApexNameForModule(module android.Module) string {
1390 if apex, ok := module.(android.ApexModule); ok {
1391 return apex.ApexName()
1392 }
1393
1394 return ""
1395}
1396
1397// Check to see if the other module is within the same named APEX as this module.
1398//
1399// If either this or the other module are on the platform then this will return
1400// false.
Paul Duffineedc5d52020-06-12 17:46:39 +01001401func withinSameApexAs(module android.ApexModule, other android.Module) bool {
Paul Duffin9b879592020-05-26 13:21:35 +01001402 name := module.ApexName()
1403 return name != "" && getApexNameForModule(other) == name
1404}
1405
Paul Duffinb05d4292020-05-20 12:19:10 +01001406func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001407 // If the client doesn't set sdk_version, but if this library prefers stubs over
1408 // the impl library, let's provide the widest API surface possible. To do so,
1409 // force override sdk_version to module_current so that the closest possible API
1410 // surface could be found in selectHeaderJarsForSdkVersion
1411 if module.defaultsToStubs() && !sdkVersion.specified() {
1412 sdkVersion = sdkSpecFrom("module_current")
1413 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001414
Paul Duffindaaa3322020-05-26 18:13:57 +01001415 // Only provide access to the implementation library if it is actually built.
1416 if module.requiresRuntimeImplementationLibrary() {
1417 // Check any special cases for java_sdk_library.
1418 //
1419 // Only allow access to the implementation library in the following condition:
1420 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001421 // * The referencing module is in the same apex as this.
Paul Duffineedc5d52020-06-12 17:46:39 +01001422 if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001423 if headerJars {
1424 return module.HeaderJars()
1425 } else {
1426 return module.ImplementationJars()
1427 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001428 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001429 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001430
Paul Duffin23970f42020-05-20 14:20:02 +01001431 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001432}
1433
Sundong Ahn241cd372018-07-13 16:16:44 +09001434// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001435func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1436 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1437}
1438
1439// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001440func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001441 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001442}
1443
Sundong Ahn80a87b32019-05-13 15:02:50 +09001444func (module *SdkLibrary) SetNoDist() {
1445 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1446}
1447
Colin Cross571cccf2019-02-04 11:22:08 -08001448var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1449
Jiyong Park82484c02018-04-23 21:41:26 +09001450func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001451 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001452 return &[]string{}
1453 }).(*[]string)
1454}
1455
Paul Duffin749f98f2019-12-30 17:23:46 +00001456func (module *SdkLibrary) getApiDir() string {
1457 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1458}
1459
Jiyong Parkc678ad32018-04-10 13:07:10 +09001460// For a java_sdk_library module, create internal modules for stubs, docs,
1461// runtime libs and xml file. If requested, the stubs and docs are created twice
1462// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001463func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1464 // If the module has been disabled then don't create any child modules.
1465 if !module.Enabled() {
1466 return
1467 }
1468
Paul Duffina18abc22020-05-16 18:54:24 +01001469 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001470 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001471 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001472 }
1473
Paul Duffin37e0b772019-12-30 17:20:10 +00001474 // If this builds against standard libraries (i.e. is not part of the core libraries)
1475 // then assume it provides both system and test apis. Otherwise, assume it does not and
1476 // also assume it does not contribute to the dist build.
1477 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1478 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001479 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001480 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1481
Inseob Kim8098faa2019-03-18 10:19:51 +09001482 missing_current_api := false
1483
Paul Duffin3375e352020-04-28 10:44:03 +01001484 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001485
Paul Duffin749f98f2019-12-30 17:23:46 +00001486 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001487 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001488 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001489 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001490 p := android.ExistentPathForSource(mctx, path)
1491 if !p.Valid() {
1492 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1493 missing_current_api = true
1494 }
1495 }
1496 }
1497
1498 if missing_current_api {
1499 script := "build/soong/scripts/gen-java-current-api-files.sh"
1500 p := android.ExistentPathForSource(mctx, script)
1501
1502 if !p.Valid() {
1503 panic(fmt.Sprintf("script file %s doesn't exist", script))
1504 }
1505
1506 mctx.ModuleErrorf("One or more current api files are missing. "+
1507 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001508 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001509 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001510 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001511 return
1512 }
1513
Paul Duffin3375e352020-04-28 10:44:03 +01001514 for _, scope := range generatedScopes {
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001515 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinc3091c82020-05-08 14:16:20 +01001516 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001517
1518 // If the args needed to generate the stubs and API are the same then they
1519 // can be generated in a single invocation of metalava, otherwise they will
1520 // need separate invocations.
1521 if scope.createStubsSourceAndApiTogether {
1522 // Use the stubs source name for legacy reasons.
1523 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1524 } else {
1525 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1526
1527 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinc3091c82020-05-08 14:16:20 +01001528 apiName := module.apiModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001529 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1530 }
1531
Paul Duffind1b3a922020-01-22 11:57:20 +00001532 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001533 }
1534
Paul Duffindfa131e2020-05-15 20:37:11 +01001535 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001536 // Create child module to create an implementation library.
1537 //
1538 // This temporarily creates a second implementation library that can be explicitly
1539 // referenced.
1540 //
1541 // TODO(b/156618935) - update comment once only one implementation library is created.
1542 module.createImplLibrary(mctx)
1543
Paul Duffindfa131e2020-05-15 20:37:11 +01001544 // Only create an XML permissions file that declares the library as being usable
1545 // as a shared library if required.
1546 if module.sharedLibrary() {
1547 module.createXmlFile(mctx)
1548 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001549
1550 // record java_sdk_library modules so that they are exported to make
1551 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1552 javaSdkLibrariesLock.Lock()
1553 defer javaSdkLibrariesLock.Unlock()
1554 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1555 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001556}
1557
1558func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001559 module.addHostAndDeviceProperties()
1560 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001561
Paul Duffin859fe962020-05-15 10:20:31 +01001562 module.initSdkLibraryComponent(&module.ModuleBase)
1563
Paul Duffina18abc22020-05-16 18:54:24 +01001564 module.properties.Installable = proptools.BoolPtr(true)
1565 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001566}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001567
Paul Duffindfa131e2020-05-15 20:37:11 +01001568func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1569 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1570}
1571
Jiyong Park932cdfe2020-05-28 00:19:53 +09001572func (module *SdkLibrary) defaultsToStubs() bool {
1573 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1574}
1575
Paul Duffin1b1e8062020-05-08 13:44:43 +01001576// Defines how to name the individual component modules the sdk library creates.
1577type sdkLibraryComponentNamingScheme interface {
1578 stubsLibraryModuleName(scope *apiScope, baseName string) string
1579
1580 stubsSourceModuleName(scope *apiScope, baseName string) string
1581
1582 apiModuleName(scope *apiScope, baseName string) string
1583}
1584
1585type defaultNamingScheme struct {
1586}
1587
1588func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1589 return scope.stubsLibraryModuleName(baseName)
1590}
1591
1592func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1593 return scope.stubsSourceModuleName(baseName)
1594}
1595
1596func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1597 return scope.apiModuleName(baseName)
1598}
1599
1600var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1601
Paul Duffin6c9c5fc2020-05-08 15:36:30 +01001602type frameworkModulesNamingScheme struct {
1603}
1604
1605func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1606 suffix := scope.name
1607 if scope == apiScopeModuleLib {
1608 suffix = "module_libs_"
1609 }
1610 return suffix
1611}
1612
1613func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1614 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1615}
1616
1617func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1618 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1619}
1620
1621func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1622 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1623}
1624
1625var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1626
Anton Hansson2d0c1942020-05-25 12:20:51 +01001627func moduleStubLinkType(name string) (stub bool, ret linkType) {
1628 // This suffix-based approach is fragile and could potentially mis-trigger.
1629 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1630 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1631 return true, javaSdk
1632 }
1633 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1634 return true, javaSystem
1635 }
1636 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1637 return true, javaModule
1638 }
1639 if strings.HasSuffix(name, ".stubs.test") {
1640 return true, javaSystem
1641 }
1642 return false, javaPlatform
1643}
1644
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001645// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1646// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1647// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1648// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1649// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001650func SdkLibraryFactory() android.Module {
1651 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001652
1653 // Initialize information common between source and prebuilt.
1654 module.initCommon(&module.ModuleBase)
1655
Inseob Kimc0907f12019-02-08 21:00:45 +09001656 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001657 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001658 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001659
1660 // Initialize the map from scope to scope specific properties.
1661 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1662 for _, scope := range allApiScopes {
1663 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1664 }
1665 module.scopeToProperties = scopeToProperties
1666
Paul Duffin4911a892020-04-29 23:35:13 +01001667 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001668 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001669 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1670 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1671
Paul Duffin1b1e8062020-05-08 13:44:43 +01001672 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001673 // If no implementation is required then it cannot be used as a shared library
1674 // either.
1675 if !module.requiresRuntimeImplementationLibrary() {
1676 // If shared_library has been explicitly set to true then it is incompatible
1677 // with api_only: true.
1678 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1679 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1680 }
1681 // Set shared_library: false.
1682 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1683 }
1684
Paul Duffin1b1e8062020-05-08 13:44:43 +01001685 if module.initCommonAfterDefaultsApplied(ctx) {
1686 module.CreateInternalModules(ctx)
1687 }
1688 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001689 return module
1690}
Colin Cross79c7c262019-04-17 11:11:46 -07001691
1692//
1693// SDK library prebuilts
1694//
1695
Paul Duffin56d44902020-01-31 13:36:25 +00001696// Properties associated with each api scope.
1697type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001698 Jars []string `android:"path"`
1699
1700 Sdk_version *string
1701
Colin Cross79c7c262019-04-17 11:11:46 -07001702 // List of shared java libs that this module has dependencies to
1703 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001704
Paul Duffinc8782502020-04-29 20:45:27 +01001705 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001706 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001707
1708 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001709 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001710
1711 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001712 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001713}
1714
Paul Duffin56d44902020-01-31 13:36:25 +00001715type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001716 // List of shared java libs, common to all scopes, that this module has
1717 // dependencies to
1718 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001719}
1720
Paul Duffineedc5d52020-06-12 17:46:39 +01001721type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001722 android.ModuleBase
1723 android.DefaultableModuleBase
1724 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001725 android.ApexModuleBase
1726 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001727
1728 properties sdkLibraryImportProperties
1729
Paul Duffin46a26a82020-04-07 19:27:04 +01001730 // Map from api scope to the scope specific property structure.
1731 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1732
Paul Duffin56d44902020-01-31 13:36:25 +00001733 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001734
1735 // The reference to the implementation library created by the source module.
1736 // Is nil if the source module does not exist.
1737 implLibraryModule *Library
1738
1739 // The reference to the xml permissions module created by the source module.
1740 // Is nil if the source module does not exist.
1741 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001742}
1743
Paul Duffineedc5d52020-06-12 17:46:39 +01001744var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001745
Paul Duffin46a26a82020-04-07 19:27:04 +01001746// The type of a structure that contains a field of type sdkLibraryScopeProperties
1747// for each apiscope in allApiScopes, e.g. something like:
1748// struct {
1749// Public sdkLibraryScopeProperties
1750// System sdkLibraryScopeProperties
1751// ...
1752// }
1753var allScopeStructType = createAllScopePropertiesStructType()
1754
1755// Dynamically create a structure type for each apiscope in allApiScopes.
1756func createAllScopePropertiesStructType() reflect.Type {
1757 var fields []reflect.StructField
1758 for _, apiScope := range allApiScopes {
1759 field := reflect.StructField{
1760 Name: apiScope.fieldName,
1761 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1762 }
1763 fields = append(fields, field)
1764 }
1765
1766 return reflect.StructOf(fields)
1767}
1768
1769// Create an instance of the scope specific structure type and return a map
1770// from apiscope to a pointer to each scope specific field.
1771func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1772 allScopePropertiesPtr := reflect.New(allScopeStructType)
1773 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1774 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1775
1776 for _, apiScope := range allApiScopes {
1777 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1778 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1779 }
1780
1781 return allScopePropertiesPtr.Interface(), scopeProperties
1782}
1783
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001784// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001785func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001786 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001787
Paul Duffin46a26a82020-04-07 19:27:04 +01001788 allScopeProperties, scopeToProperties := createPropertiesInstance()
1789 module.scopeProperties = scopeToProperties
1790 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001791
Paul Duffinc3091c82020-05-08 14:16:20 +01001792 // Initialize information common between source and prebuilt.
1793 module.initCommon(&module.ModuleBase)
1794
Paul Duffin0bdcb272020-02-06 15:24:57 +00001795 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001796 android.InitApexModule(module)
1797 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001798 InitJavaModule(module, android.HostAndDeviceSupported)
1799
Paul Duffin1b1e8062020-05-08 13:44:43 +01001800 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1801 if module.initCommonAfterDefaultsApplied(mctx) {
1802 module.createInternalModules(mctx)
1803 }
1804 })
Colin Cross79c7c262019-04-17 11:11:46 -07001805 return module
1806}
1807
Paul Duffineedc5d52020-06-12 17:46:39 +01001808func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001809 return &module.prebuilt
1810}
1811
Paul Duffineedc5d52020-06-12 17:46:39 +01001812func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001813 return module.prebuilt.Name(module.ModuleBase.Name())
1814}
1815
Paul Duffineedc5d52020-06-12 17:46:39 +01001816func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001817
Paul Duffin50061512020-01-21 16:31:05 +00001818 // If the build is configured to use prebuilts then force this to be preferred.
1819 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1820 module.prebuilt.ForcePrefer()
1821 }
1822
Paul Duffin46a26a82020-04-07 19:27:04 +01001823 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001824 if len(scopeProperties.Jars) == 0 {
1825 continue
1826 }
1827
Paul Duffinbbb546b2020-04-09 00:07:11 +01001828 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001829
Paul Duffin0f8faff2020-05-20 16:18:00 +01001830 if len(scopeProperties.Stub_srcs) > 0 {
1831 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1832 }
Paul Duffin56d44902020-01-31 13:36:25 +00001833 }
Colin Cross79c7c262019-04-17 11:11:46 -07001834
1835 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1836 javaSdkLibrariesLock.Lock()
1837 defer javaSdkLibrariesLock.Unlock()
1838 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1839}
1840
Paul Duffineedc5d52020-06-12 17:46:39 +01001841func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001842 // Creates a java import for the jar with ".stubs" suffix
1843 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001844 Name *string
1845 Sdk_version *string
1846 Libs []string
1847 Jars []string
1848 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001849 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001850 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001851 props.Sdk_version = scopeProperties.Sdk_version
1852 // Prepend any of the libs from the legacy public properties to the libs for each of the
1853 // scopes to avoid having to duplicate them in each scope.
1854 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1855 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001856
Paul Duffin38b57852020-05-13 16:08:09 +01001857 // The imports are preferred if the java_sdk_library_import is preferred.
1858 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001859
1860 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001861}
1862
Paul Duffineedc5d52020-06-12 17:46:39 +01001863func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001864 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001865 Name *string
1866 Srcs []string
1867 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001868 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001869 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001870 props.Srcs = scopeProperties.Stub_srcs
1871 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001872
1873 // The stubs source is preferred if the java_sdk_library_import is preferred.
1874 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001875}
1876
Paul Duffineedc5d52020-06-12 17:46:39 +01001877func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001878 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001879 if len(scopeProperties.Jars) == 0 {
1880 continue
1881 }
1882
1883 // Add dependencies to the prebuilt stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001884 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001885
1886 if len(scopeProperties.Stub_srcs) > 0 {
1887 // Add dependencies to the prebuilt stubs source library
1888 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1889 }
Paul Duffin56d44902020-01-31 13:36:25 +00001890 }
Paul Duffineedc5d52020-06-12 17:46:39 +01001891
1892 implName := module.implLibraryModuleName()
1893 if ctx.OtherModuleExists(implName) {
1894 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1895
1896 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1897 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1898 // Add dependency to the rule for generating the xml permissions file
1899 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1900 }
1901 }
Colin Cross79c7c262019-04-17 11:11:46 -07001902}
1903
Paul Duffineedc5d52020-06-12 17:46:39 +01001904func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1905 depTag := mctx.OtherModuleDependencyTag(dep)
1906 if depTag == xmlPermissionsFileTag {
1907 return true
1908 }
1909
1910 // None of the other dependencies of the java_sdk_library_import are in the same apex
1911 // as the one that references this module.
1912 return false
1913}
1914
Jooyung Han749dc692020-04-15 11:03:39 +09001915func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
1916 // we don't check prebuilt modules for sdk_version
1917 return nil
1918}
1919
Paul Duffineedc5d52020-06-12 17:46:39 +01001920func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001921 return module.commonOutputFiles(tag)
1922}
1923
Paul Duffineedc5d52020-06-12 17:46:39 +01001924func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin0f8faff2020-05-20 16:18:00 +01001925 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001926 ctx.VisitDirectDeps(func(to android.Module) {
1927 tag := ctx.OtherModuleDependencyTag(to)
1928
Paul Duffin0f8faff2020-05-20 16:18:00 +01001929 // Extract information from any of the scope specific dependencies.
1930 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1931 apiScope := scopeTag.apiScope
1932 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1933
1934 // Extract information from the dependency. The exact information extracted
1935 // is determined by the nature of the dependency which is determined by the tag.
1936 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01001937 } else if tag == implLibraryTag {
1938 if implLibrary, ok := to.(*Library); ok {
1939 module.implLibraryModule = implLibrary
1940 } else {
1941 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1942 }
1943 } else if tag == xmlPermissionsFileTag {
1944 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1945 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1946 } else {
1947 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1948 }
Colin Cross79c7c262019-04-17 11:11:46 -07001949 }
1950 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001951
1952 // Populate the scope paths with information from the properties.
1953 for apiScope, scopeProperties := range module.scopeProperties {
1954 if len(scopeProperties.Jars) == 0 {
1955 continue
1956 }
1957
1958 paths := module.getScopePathsCreateIfNeeded(apiScope)
1959 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1960 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1961 }
Colin Cross79c7c262019-04-17 11:11:46 -07001962}
1963
Paul Duffineedc5d52020-06-12 17:46:39 +01001964func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1965
1966 // For consistency with SdkLibrary make the implementation jar available to libraries that
1967 // are within the same APEX.
1968 implLibraryModule := module.implLibraryModule
1969 if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
1970 if headerJars {
1971 return implLibraryModule.HeaderJars()
1972 } else {
1973 return implLibraryModule.ImplementationJars()
1974 }
1975 }
1976
Paul Duffin23970f42020-05-20 14:20:02 +01001977 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001978}
1979
Colin Cross79c7c262019-04-17 11:11:46 -07001980// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001981func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001982 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001983 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07001984}
1985
1986// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001987func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001988 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001989 return module.sdkJars(ctx, sdkVersion, false)
1990}
1991
1992// to satisfy apex.javaDependency interface
1993func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
1994 if module.implLibraryModule == nil {
1995 return nil
1996 } else {
1997 return module.implLibraryModule.DexJarBuildPath()
1998 }
1999}
2000
2001// to satisfy apex.javaDependency interface
2002func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2003 if module.implLibraryModule == nil {
2004 return nil
2005 } else {
2006 return module.implLibraryModule.JacocoReportClassesFile()
2007 }
2008}
2009
2010// to satisfy apex.javaDependency interface
2011func (module *SdkLibraryImport) Stem() string {
2012 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002013}
Jiyong Parke3833882020-02-17 17:28:10 +09002014
Paul Duffin44b481b2020-06-17 16:59:43 +01002015var _ ApexDependency = (*SdkLibraryImport)(nil)
2016
2017// to satisfy java.ApexDependency interface
2018func (module *SdkLibraryImport) HeaderJars() android.Paths {
2019 if module.implLibraryModule == nil {
2020 return nil
2021 } else {
2022 return module.implLibraryModule.HeaderJars()
2023 }
2024}
2025
2026// to satisfy java.ApexDependency interface
2027func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2028 if module.implLibraryModule == nil {
2029 return nil
2030 } else {
2031 return module.implLibraryModule.ImplementationAndResourcesJars()
2032 }
2033}
2034
Jiyong Parke3833882020-02-17 17:28:10 +09002035//
2036// java_sdk_library_xml
2037//
2038type sdkLibraryXml struct {
2039 android.ModuleBase
2040 android.DefaultableModuleBase
2041 android.ApexModuleBase
2042
2043 properties sdkLibraryXmlProperties
2044
2045 outputFilePath android.OutputPath
2046 installDirPath android.InstallPath
2047}
2048
2049type sdkLibraryXmlProperties struct {
2050 // canonical name of the lib
2051 Lib_name *string
2052}
2053
2054// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2055// Not to be used directly by users. java_sdk_library internally uses this.
2056func sdkLibraryXmlFactory() android.Module {
2057 module := &sdkLibraryXml{}
2058
2059 module.AddProperties(&module.properties)
2060
2061 android.InitApexModule(module)
2062 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2063
2064 return module
2065}
2066
2067// from android.PrebuiltEtcModule
2068func (module *sdkLibraryXml) SubDir() string {
2069 return "permissions"
2070}
2071
2072// from android.PrebuiltEtcModule
2073func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2074 return module.outputFilePath
2075}
2076
2077// from android.ApexModule
2078func (module *sdkLibraryXml) AvailableFor(what string) bool {
2079 return true
2080}
2081
2082func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2083 // do nothing
2084}
2085
Jooyung Han749dc692020-04-15 11:03:39 +09002086func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
2087 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2088 return nil
2089}
2090
Jiyong Parke3833882020-02-17 17:28:10 +09002091// File path to the runtime implementation library
2092func (module *sdkLibraryXml) implPath() string {
2093 implName := proptools.String(module.properties.Lib_name)
2094 if apexName := module.ApexName(); apexName != "" {
2095 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
2096 // In most cases, this works fine. But when apex_name is set or override_apex is used
2097 // this can be wrong.
2098 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2099 }
2100 partition := "system"
2101 if module.SocSpecific() {
2102 partition = "vendor"
2103 } else if module.DeviceSpecific() {
2104 partition = "odm"
2105 } else if module.ProductSpecific() {
2106 partition = "product"
2107 } else if module.SystemExtSpecific() {
2108 partition = "system_ext"
2109 }
2110 return "/" + partition + "/framework/" + implName + ".jar"
2111}
2112
2113func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2114 libName := proptools.String(module.properties.Lib_name)
2115 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2116
2117 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2118 rule := android.NewRuleBuilder()
2119 rule.Command().
2120 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2121 Output(module.outputFilePath)
2122
2123 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2124
2125 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2126}
2127
2128func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2129 if !module.IsForPlatform() {
2130 return []android.AndroidMkEntries{android.AndroidMkEntries{
2131 Disabled: true,
2132 }}
2133 }
2134
2135 return []android.AndroidMkEntries{android.AndroidMkEntries{
2136 Class: "ETC",
2137 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2138 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2139 func(entries *android.AndroidMkEntries) {
2140 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2141 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2142 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2143 },
2144 },
2145 }}
2146}
Paul Duffindd46f712020-02-10 13:37:10 +00002147
2148type sdkLibrarySdkMemberType struct {
2149 android.SdkMemberTypeBase
2150}
2151
2152func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2153 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2154}
2155
2156func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2157 _, ok := module.(*SdkLibrary)
2158 return ok
2159}
2160
2161func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2162 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2163}
2164
2165func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2166 return &sdkLibrarySdkMemberProperties{}
2167}
2168
2169type sdkLibrarySdkMemberProperties struct {
2170 android.SdkMemberPropertiesBase
2171
2172 // Scope to per scope properties.
2173 Scopes map[*apiScope]scopeProperties
2174
2175 // Additional libraries that the exported stubs libraries depend upon.
2176 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002177
2178 // The Java stubs source files.
2179 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002180
2181 // The naming scheme.
2182 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002183
2184 // True if the java_sdk_library_import is for a shared library, false
2185 // otherwise.
2186 Shared_library *bool
Paul Duffindd46f712020-02-10 13:37:10 +00002187}
2188
2189type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002190 Jars android.Paths
2191 StubsSrcJar android.Path
2192 CurrentApiFile android.Path
2193 RemovedApiFile android.Path
2194 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002195}
2196
2197func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2198 sdk := variant.(*SdkLibrary)
2199
2200 s.Scopes = make(map[*apiScope]scopeProperties)
2201 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002202 paths := sdk.findScopePaths(apiScope)
2203 if paths == nil {
2204 continue
2205 }
2206
Paul Duffindd46f712020-02-10 13:37:10 +00002207 jars := paths.stubsImplPath
2208 if len(jars) > 0 {
2209 properties := scopeProperties{}
2210 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002211 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002212 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002213 if paths.currentApiFilePath.Valid() {
2214 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2215 }
2216 if paths.removedApiFilePath.Valid() {
2217 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2218 }
Paul Duffindd46f712020-02-10 13:37:10 +00002219 s.Scopes[apiScope] = properties
2220 }
2221 }
2222
2223 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01002224 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002225 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffindd46f712020-02-10 13:37:10 +00002226}
2227
2228func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002229 if s.Naming_scheme != nil {
2230 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2231 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002232 if s.Shared_library != nil {
2233 propertySet.AddProperty("shared_library", *s.Shared_library)
2234 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002235
Paul Duffindd46f712020-02-10 13:37:10 +00002236 for _, apiScope := range allApiScopes {
2237 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002238 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002239
Paul Duffin3d1248c2020-04-09 00:10:17 +01002240 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2241
Paul Duffindd46f712020-02-10 13:37:10 +00002242 var jars []string
2243 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002244 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002245 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2246 jars = append(jars, dest)
2247 }
2248 scopeSet.AddProperty("jars", jars)
2249
Paul Duffin3d1248c2020-04-09 00:10:17 +01002250 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2251 // the source files are also unpacked.
2252 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2253 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2254 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2255
Paul Duffin1fd005d2020-04-09 01:08:11 +01002256 if properties.CurrentApiFile != nil {
2257 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2258 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2259 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2260 }
2261
2262 if properties.RemovedApiFile != nil {
2263 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002264 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002265 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2266 }
2267
Paul Duffindd46f712020-02-10 13:37:10 +00002268 if properties.SdkVersion != "" {
2269 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2270 }
2271 }
2272 }
2273
2274 if len(s.Libs) > 0 {
2275 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2276 }
2277}