blob: 684a0026f70e46e040d3ab79ea4d9f375e921a2c [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,
267 // Module_lib scope is disabled by default in legacy mode.
268 //
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 Duffind1b3a922020-01-22 11:57:20 +0000283 allApiScopes = apiScopes{
284 apiScopePublic,
285 apiScopeSystem,
286 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100287 apiScopeModuleLib,
Paul Duffind1b3a922020-01-22 11:57:20 +0000288 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900289)
290
Jiyong Park82484c02018-04-23 21:41:26 +0900291var (
292 javaSdkLibrariesLock sync.Mutex
293)
294
Jiyong Parkc678ad32018-04-10 13:07:10 +0900295// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900296// 1) disallowing linking to the runtime shared lib
297// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900298
299func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000300 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900301
Jiyong Park82484c02018-04-23 21:41:26 +0900302 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
303 javaSdkLibraries := javaSdkLibraries(ctx.Config())
304 sort.Strings(*javaSdkLibraries)
305 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
306 })
Paul Duffindd46f712020-02-10 13:37:10 +0000307
308 // Register sdk member types.
309 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
310 android.SdkMemberTypeBase{
311 PropertyName: "java_sdk_libs",
312 SupportsSdk: true,
313 },
314 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900315}
316
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000317func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
318 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
319 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
320}
321
Paul Duffin3375e352020-04-28 10:44:03 +0100322// Properties associated with each api scope.
323type ApiScopeProperties struct {
324 // Indicates whether the api surface is generated.
325 //
326 // If this is set for any scope then all scopes must explicitly specify if they
327 // are enabled. This is to prevent new usages from depending on legacy behavior.
328 //
329 // Otherwise, if this is not set for any scope then the default behavior is
330 // scope specific so please refer to the scope specific property documentation.
331 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100332
333 // The sdk_version to use for building the stubs.
334 //
335 // If not specified then it will use an sdk_version determined as follows:
336 // 1) If the sdk_version specified on the java_sdk_library is none then this
337 // will be none. This is used for java_sdk_library instances that are used
338 // to create stubs that contribute to the core_current sdk version.
339 // 2) Otherwise, it is assumed that this library extends but does not contribute
340 // directly to a specific sdk_version and so this uses the sdk_version appropriate
341 // for the api scope. e.g. public will use sdk_version: current, system will use
342 // sdk_version: system_current, etc.
343 //
344 // This does not affect the sdk_version used for either generating the stubs source
345 // or the API file. They both have to use the same sdk_version as is used for
346 // compiling the implementation library.
347 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100348}
349
Jiyong Parkc678ad32018-04-10 13:07:10 +0900350type sdkLibraryProperties struct {
Paul Duffin4911a892020-04-29 23:35:13 +0100351 // Visibility for stubs library modules. If not specified then defaults to the
352 // visibility property.
353 Stubs_library_visibility []string
354
355 // Visibility for stubs source modules. If not specified then defaults to the
356 // visibility property.
357 Stubs_source_visibility []string
358
Sundong Ahnf043cf62018-06-25 16:04:37 +0900359 // List of Java libraries that will be in the classpath when building stubs
360 Stub_only_libs []string `android:"arch_variant"`
361
Paul Duffin7a586d32019-12-30 17:09:34 +0000362 // list of package names that will be documented and publicized as API.
363 // This allows the API to be restricted to a subset of the source files provided.
364 // If this is unspecified then all the source files will be treated as being part
365 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900366 Api_packages []string
367
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900368 // list of package names that must be hidden from the API
369 Hidden_api_packages []string
370
Paul Duffin749f98f2019-12-30 17:23:46 +0000371 // the relative path to the directory containing the api specification files.
372 // Defaults to "api".
373 Api_dir *string
374
Paul Duffin43db9be2019-12-30 17:35:49 +0000375 // If set to true there is no runtime library.
376 Api_only *bool
377
Paul Duffin11512472019-02-11 15:55:17 +0000378 // local files that are used within user customized droiddoc options.
379 Droiddoc_option_files []string
380
381 // additional droiddoc options
382 // Available variables for substitution:
383 //
384 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900385 Droiddoc_options []string
386
Sundong Ahn054b19a2018-10-19 13:46:09 +0900387 // a list of top-level directories containing files to merge qualifier annotations
388 // (i.e. those intended to be included in the stubs written) from.
389 Merge_annotations_dirs []string
390
391 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
392 Merge_inclusion_annotations_dirs []string
393
394 // If set to true, the path of dist files is apistubs/core. Defaults to false.
395 Core_lib *bool
396
Sundong Ahn80a87b32019-05-13 15:02:50 +0900397 // don't create dist rules.
398 No_dist *bool `blueprint:"mutated"`
399
Paul Duffin3375e352020-04-28 10:44:03 +0100400 // indicates whether system and test apis should be generated.
401 Generate_system_and_test_apis bool `blueprint:"mutated"`
402
403 // The properties specific to the public api scope
404 //
405 // Unless explicitly specified by using public.enabled the public api scope is
406 // enabled by default in both legacy and non-legacy mode.
407 Public ApiScopeProperties
408
409 // The properties specific to the system api scope
410 //
411 // In legacy mode the system api scope is enabled by default when sdk_version
412 // is set to something other than "none".
413 //
414 // In non-legacy mode the system api scope is disabled by default.
415 System ApiScopeProperties
416
417 // The properties specific to the test api scope
418 //
419 // In legacy mode the test api scope is enabled by default when sdk_version
420 // is set to something other than "none".
421 //
422 // In non-legacy mode the test api scope is disabled by default.
423 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000424
Paul Duffin8f265b92020-04-28 14:13:56 +0100425 // The properties specific to the module_lib api scope
426 //
427 // Unless explicitly specified by using test.enabled the module_lib api scope is
428 // disabled by default.
429 Module_lib ApiScopeProperties
430
Paul Duffin160fe412020-05-10 19:32:20 +0100431 // Properties related to api linting.
432 Api_lint struct {
433 // Enable api linting.
434 Enabled *bool
435 }
436
Jiyong Parkc678ad32018-04-10 13:07:10 +0900437 // TODO: determines whether to create HTML doc or not
438 //Html_doc *bool
439}
440
Paul Duffin0f8faff2020-05-20 16:18:00 +0100441// Paths to outputs from java_sdk_library and java_sdk_library_import.
442//
443// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
444// OptionalPaths are always set by java_sdk_library but may not be set by
445// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000446type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100447 // The path (represented as Paths for convenience when returning) to the stubs header jar.
448 //
449 // That is the jar that is created by turbine.
450 stubsHeaderPath android.Paths
451
452 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
453 //
454 // This is not the implementation jar, it still only contains stubs.
455 stubsImplPath android.Paths
456
457 // The API specification file, e.g. system_current.txt.
458 currentApiFilePath android.OptionalPath
459
460 // The specification of API elements removed since the last release.
461 removedApiFilePath android.OptionalPath
462
463 // The stubs source jar.
464 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000465}
466
Paul Duffinc8782502020-04-29 20:45:27 +0100467func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
468 if lib, ok := dep.(Dependency); ok {
469 paths.stubsHeaderPath = lib.HeaderJars()
470 paths.stubsImplPath = lib.ImplementationJars()
471 return nil
472 } else {
473 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
474 }
475}
476
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100477func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
478 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
479 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100480 return nil
481 } else {
482 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
483 }
484}
485
Paul Duffin0f8faff2020-05-20 16:18:00 +0100486func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
487 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
488 action(apiStubsProvider)
489 return nil
490 } else {
491 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
492 }
493}
494
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100495func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100496 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
497 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100498}
499
500func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
501 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
502 paths.extractApiInfoFromApiStubsProvider(provider)
503 })
504}
505
Paul Duffin0f8faff2020-05-20 16:18:00 +0100506func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
507 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100508}
509
510func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100511 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100512 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
513 })
514}
515
516func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
517 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
518 paths.extractApiInfoFromApiStubsProvider(provider)
519 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
520 })
521}
522
523type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100524 // The naming scheme to use for the components that this module creates.
525 //
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100526 // If not specified then it defaults to "default". The other allowable value is
527 // "framework-modules" which matches the scheme currently used by framework modules
528 // for the equivalent components represented as separate Soong modules.
Paul Duffin1b1e8062020-05-08 13:44:43 +0100529 //
530 // This is a temporary mechanism to simplify conversion from separate modules for each
531 // component that follow a different naming pattern to the default one.
532 //
533 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100534 Naming_scheme *string
535}
536
Paul Duffin56d44902020-01-31 13:36:25 +0000537// Common code between sdk library and sdk library import
538type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100539 moduleBase *android.ModuleBase
540
Paul Duffin56d44902020-01-31 13:36:25 +0000541 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100542
543 namingScheme sdkLibraryComponentNamingScheme
544
545 commonProperties commonToSdkLibraryAndImportProperties
Paul Duffin56d44902020-01-31 13:36:25 +0000546}
547
Paul Duffinc3091c82020-05-08 14:16:20 +0100548func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
549 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100550
551 moduleBase.AddProperties(&c.commonProperties)
552}
553
554func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
555 schemeProperty := proptools.StringDefault(c.commonProperties.Naming_scheme, "default")
556 switch schemeProperty {
557 case "default":
558 c.namingScheme = &defaultNamingScheme{}
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100559 case "framework-modules":
560 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1b1e8062020-05-08 13:44:43 +0100561 default:
562 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
563 return false
564 }
565
566 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100567}
568
569// Name of the java_library module that compiles the stubs source.
570func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100571 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100572}
573
574// Name of the droidstubs module that generates the stubs source and may also
575// generate/check the API.
576func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100577 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100578}
579
580// Name of the droidstubs module that generates/checks the API. Only used if it
581// requires different arts to the stubs source generating module.
582func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100583 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100584}
585
Paul Duffin46dc45a2020-05-14 15:39:10 +0100586// The component names for different outputs of the java_sdk_library.
587//
588// They are similar to the names used for the child modules it creates
589const (
590 stubsSourceComponentName = "stubs.source"
591
592 apiTxtComponentName = "api.txt"
593
594 removedApiTxtComponentName = "removed-api.txt"
595)
596
597// A regular expression to match tags that reference a specific stubs component.
598//
599// It will only match if given a valid scope and a valid component. It is verfy strict
600// to ensure it does not accidentally match a similar looking tag that should be processed
601// by the embedded Library.
602var tagSplitter = func() *regexp.Regexp {
603 // Given a list of literal string items returns a regular expression that will
604 // match any one of the items.
605 choice := func(items ...string) string {
606 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
607 }
608
609 // Regular expression to match one of the scopes.
610 scopesRegexp := choice(allScopeNames...)
611
612 // Regular expression to match one of the components.
613 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
614
615 // Regular expression to match any combination of one scope and one component.
616 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
617}()
618
619// For OutputFileProducer interface
620//
621// .<scope>.stubs.source
622// .<scope>.api.txt
623// .<scope>.removed-api.txt
624func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
625 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
626 scopeName := groups[1]
627 component := groups[2]
628
629 if scope, ok := scopeByName[scopeName]; ok {
630 paths := c.findScopePaths(scope)
631 if paths == nil {
632 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
633 }
634
635 switch component {
636 case stubsSourceComponentName:
637 if paths.stubsSrcJar.Valid() {
638 return android.Paths{paths.stubsSrcJar.Path()}, nil
639 }
640
641 case apiTxtComponentName:
642 if paths.currentApiFilePath.Valid() {
643 return android.Paths{paths.currentApiFilePath.Path()}, nil
644 }
645
646 case removedApiTxtComponentName:
647 if paths.removedApiFilePath.Valid() {
648 return android.Paths{paths.removedApiFilePath.Path()}, nil
649 }
650 }
651
652 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
653 } else {
654 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
655 }
656
657 } else {
658 return nil, nil
659 }
660}
661
Paul Duffin803a9562020-05-20 11:52:25 +0100662func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000663 if c.scopePaths == nil {
664 c.scopePaths = make(map[*apiScope]*scopePaths)
665 }
666 paths := c.scopePaths[scope]
667 if paths == nil {
668 paths = &scopePaths{}
669 c.scopePaths[scope] = paths
670 }
671
672 return paths
673}
674
Paul Duffin803a9562020-05-20 11:52:25 +0100675func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
676 if c.scopePaths == nil {
677 return nil
678 }
679
680 return c.scopePaths[scope]
681}
682
683// If this does not support the requested api scope then find the closest available
684// scope it does support. Returns nil if no such scope is available.
685func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
686 for s := scope; s != nil; s = s.extends {
687 if paths := c.findScopePaths(s); paths != nil {
688 return paths
689 }
690 }
691
692 // This should never happen outside tests as public should be the base scope for every
693 // scope and is enabled by default.
694 return nil
695}
696
Paul Duffinb05d4292020-05-20 12:19:10 +0100697func (c *commonToSdkLibraryAndImport) sdkJarsCommon(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
698
699 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
700 if sdkVersion.version.isNumbered() {
701 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
702 }
703
704 var apiScope *apiScope
705 switch sdkVersion.kind {
706 case sdkSystem:
707 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100708 case sdkModule:
709 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100710 case sdkTest:
711 apiScope = apiScopeTest
712 default:
713 apiScope = apiScopePublic
714 }
715
Paul Duffin803a9562020-05-20 11:52:25 +0100716 paths := c.findClosestScopePath(apiScope)
717 if paths == nil {
718 var scopes []string
719 for _, s := range allApiScopes {
720 if c.findScopePaths(s) != nil {
721 scopes = append(scopes, s.name)
722 }
723 }
724 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
725 return nil
726 }
727
Paul Duffinb05d4292020-05-20 12:19:10 +0100728 if headerJars {
729 return paths.stubsHeaderPath
730 } else {
731 return paths.stubsImplPath
732 }
733}
734
Inseob Kimc0907f12019-02-08 21:00:45 +0900735type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900736 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900737
Sundong Ahn054b19a2018-10-19 13:46:09 +0900738 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900739
Paul Duffin3375e352020-04-28 10:44:03 +0100740 // Map from api scope to the scope specific property structure.
741 scopeToProperties map[*apiScope]*ApiScopeProperties
742
Paul Duffin56d44902020-01-31 13:36:25 +0000743 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900744}
745
Inseob Kimc0907f12019-02-08 21:00:45 +0900746var _ Dependency = (*SdkLibrary)(nil)
747var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800748
Paul Duffin3375e352020-04-28 10:44:03 +0100749func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
750 return module.sdkLibraryProperties.Generate_system_and_test_apis
751}
752
753func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
754 // Check to see if any scopes have been explicitly enabled. If any have then all
755 // must be.
756 anyScopesExplicitlyEnabled := false
757 for _, scope := range allApiScopes {
758 scopeProperties := module.scopeToProperties[scope]
759 if scopeProperties.Enabled != nil {
760 anyScopesExplicitlyEnabled = true
761 break
762 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000763 }
Paul Duffin3375e352020-04-28 10:44:03 +0100764
765 var generatedScopes apiScopes
766 enabledScopes := make(map[*apiScope]struct{})
767 for _, scope := range allApiScopes {
768 scopeProperties := module.scopeToProperties[scope]
769 // If any scopes are explicitly enabled then ignore the legacy enabled status.
770 // This is to ensure that any new usages of this module type do not rely on legacy
771 // behaviour.
772 defaultEnabledStatus := false
773 if anyScopesExplicitlyEnabled {
774 defaultEnabledStatus = scope.defaultEnabledStatus
775 } else {
776 defaultEnabledStatus = scope.legacyEnabledStatus(module)
777 }
778 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
779 if enabled {
780 enabledScopes[scope] = struct{}{}
781 generatedScopes = append(generatedScopes, scope)
782 }
783 }
784
785 // Now check to make sure that any scope that is extended by an enabled scope is also
786 // enabled.
787 for _, scope := range allApiScopes {
788 if _, ok := enabledScopes[scope]; ok {
789 extends := scope.extends
790 if extends != nil {
791 if _, ok := enabledScopes[extends]; !ok {
792 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
793 }
794 }
795 }
796 }
797
798 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000799}
800
Paul Duffine74ac732020-02-06 13:51:46 +0000801var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
802
Jiyong Parke3833882020-02-17 17:28:10 +0900803func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
804 if dt, ok := depTag.(dependencyTag); ok {
805 return dt == xmlPermissionsFileTag
806 }
807 return false
808}
809
Inseob Kimc0907f12019-02-08 21:00:45 +0900810func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100811 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000812 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100813 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000814
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100815 // If the stubs source and API cannot be generated together then add an additional dependency on
816 // the API module.
817 if apiScope.createStubsSourceAndApiTogether {
818 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinc3091c82020-05-08 14:16:20 +0100819 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100820 } else {
821 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinc3091c82020-05-08 14:16:20 +0100822 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
823 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100824 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900825 }
826
Paul Duffine74ac732020-02-06 13:51:46 +0000827 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
828 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900829 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000830 }
831
Sundong Ahn054b19a2018-10-19 13:46:09 +0900832 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900833}
834
Paul Duffin46dc45a2020-05-14 15:39:10 +0100835func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
836 paths, err := module.commonOutputFiles(tag)
837 if paths == nil && err == nil {
838 return module.Library.OutputFiles(tag)
839 } else {
840 return paths, err
841 }
842}
843
Inseob Kimc0907f12019-02-08 21:00:45 +0900844func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000845 // Don't build an implementation library if this is api only.
846 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
847 module.Library.GenerateAndroidBuildActions(ctx)
848 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900849
Sundong Ahn57368eb2018-07-06 11:20:23 +0900850 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000851 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900852 // the recorded paths will be returned depending on the link type of the caller.
853 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900854 tag := ctx.OtherModuleDependencyTag(to)
855
Paul Duffinc8782502020-04-29 20:45:27 +0100856 // Extract information from any of the scope specific dependencies.
857 if scopeTag, ok := tag.(scopeDependencyTag); ok {
858 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +0100859 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +0100860
861 // Extract information from the dependency. The exact information extracted
862 // is determined by the nature of the dependency which is determined by the tag.
863 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900864 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900865 })
866}
867
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900868func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000869 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
870 return nil
871 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900872 entriesList := module.Library.AndroidMkEntries()
873 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700874 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900875 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900876}
877
Jiyong Parkc678ad32018-04-10 13:07:10 +0900878// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900879func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900880 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900881}
882
Jiyong Parkc678ad32018-04-10 13:07:10 +0900883// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900884func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900885 return module.BaseModuleName() + sdkXmlFileSuffix
886}
887
Anton Hansson5fd5d242020-03-27 19:43:19 +0000888// The dist path of the stub artifacts
889func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
890 if module.ModuleBase.Owner() != "" {
891 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
892 } else if Bool(module.sdkLibraryProperties.Core_lib) {
893 return path.Join("apistubs", "core", apiScope.name)
894 } else {
895 return path.Join("apistubs", "android", apiScope.name)
896 }
897}
898
Paul Duffin12ceb462019-12-24 20:31:31 +0000899// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +0100900func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +0100901 scopeProperties := module.scopeToProperties[apiScope]
902 if scopeProperties.Sdk_version != nil {
903 return proptools.String(scopeProperties.Sdk_version)
904 }
905
Paul Duffin12ceb462019-12-24 20:31:31 +0000906 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
907 if sdkDep.hasStandardLibs() {
908 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000909 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000910 } else {
911 // Otherwise, use no system module.
912 return "none"
913 }
914}
915
Paul Duffind1b3a922020-01-22 11:57:20 +0000916func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
917 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900918}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900919
Paul Duffind1b3a922020-01-22 11:57:20 +0000920func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
921 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900922}
923
924// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100925func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900926 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +0100927 Name *string
928 Visibility []string
929 Srcs []string
930 Installable *bool
931 Sdk_version *string
932 System_modules *string
933 Patch_module *string
934 Libs []string
935 Compile_dex *bool
936 Java_version *string
937 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900938 Pdk struct {
939 Enabled *bool
940 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900941 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900942 Openjdk9 struct {
943 Srcs []string
944 Javacflags []string
945 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000946 Dist struct {
947 Targets []string
948 Dest *string
949 Dir *string
950 Tag *string
951 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900952 }{}
953
Paul Duffinc3091c82020-05-08 14:16:20 +0100954 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100955
956 // If stubs_library_visibility is not set then the created module will use the
957 // visibility of this module.
958 visibility := module.sdkLibraryProperties.Stubs_library_visibility
959 props.Visibility = visibility
960
Jiyong Parkc678ad32018-04-10 13:07:10 +0900961 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +0100962 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000963 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100964 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +0100965 props.System_modules = module.deviceProperties.System_modules
966 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000967 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900968 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900969 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffina18abc22020-05-16 18:54:24 +0100970 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
971 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
972 props.Java_version = module.properties.Java_version
973 if module.deviceProperties.Compile_dex != nil {
974 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900975 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900976
Anton Hansson5fd5d242020-03-27 19:43:19 +0000977 // Dist the class jar artifact for sdk builds.
978 if !Bool(module.sdkLibraryProperties.No_dist) {
979 props.Dist.Targets = []string{"sdk", "win_sdk"}
980 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
981 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
982 props.Dist.Tag = proptools.StringPtr(".jar")
983 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900984
Colin Cross84dfc3d2019-09-25 11:33:01 -0700985 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900986}
987
Paul Duffin6d0886e2020-04-07 18:49:53 +0100988// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +0100989// files and also updates and checks the API specification files.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100990func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900991 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900992 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100993 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900994 Srcs []string
995 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100996 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000997 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900998 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000999 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001000 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001001 Java_version *string
1002 Merge_annotations_dirs []string
1003 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001004 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001005 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001006 Current ApiToCheck
1007 Last_released ApiToCheck
1008 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001009
1010 Api_lint struct {
1011 Enabled *bool
1012 New_since *string
1013 Baseline_file *string
1014 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001015 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001016 Aidl struct {
1017 Include_dirs []string
1018 Local_include_dirs []string
1019 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001020 Dist struct {
1021 Targets []string
1022 Dest *string
1023 Dir *string
1024 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001025 }{}
1026
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001027 // The stubs source processing uses the same compile time classpath when extracting the
1028 // API from the implementation library as it does when compiling it. i.e. the same
1029 // * sdk version
1030 // * system_modules
1031 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001032
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001033 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +01001034
1035 // If stubs_source_visibility is not set then the created module will use the
1036 // visibility of this module.
1037 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1038 props.Visibility = visibility
1039
Paul Duffina18abc22020-05-16 18:54:24 +01001040 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1041 props.Sdk_version = module.deviceProperties.Sdk_version
1042 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001043 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001044 // A droiddoc module has only one Libs property and doesn't distinguish between
1045 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001046 props.Libs = module.properties.Libs
1047 props.Libs = append(props.Libs, module.properties.Static_libs...)
1048 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1049 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1050 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001051
Sundong Ahn054b19a2018-10-19 13:46:09 +09001052 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1053 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1054
Paul Duffin6d0886e2020-04-07 18:49:53 +01001055 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001056 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001057 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001058 }
1059 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001060 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001061 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1062 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001063 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001064 disabledWarnings := []string{
1065 "MissingPermission",
1066 "BroadcastBehavior",
1067 "HiddenSuperclass",
1068 "DeprecationMismatch",
1069 "UnavailableSymbol",
1070 "SdkConstant",
1071 "HiddenTypeParameter",
1072 "Todo",
1073 "Typo",
1074 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001075 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001076
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001077 if !createStubSources {
1078 // Stubs are not required.
1079 props.Generate_stubs = proptools.BoolPtr(false)
1080 }
1081
Paul Duffin1fb487d2020-04-07 18:50:10 +01001082 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001083 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001084 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001085 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001086
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001087 if createApi {
1088 // List of APIs identified from the provided source files are created. They are later
1089 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1090 // last-released (a.k.a numbered) list of API.
1091 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1092 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1093 apiDir := module.getApiDir()
1094 currentApiFileName = path.Join(apiDir, currentApiFileName)
1095 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001096
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001097 // check against the not-yet-release API
1098 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1099 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001100
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001101 if !apiScope.unstable {
1102 // check against the latest released API
1103 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1104 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1105 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1106 module.latestRemovedApiFilegroupName(apiScope))
1107 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001108
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001109 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1110 // Enable api lint.
1111 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1112 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001113
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001114 // If it exists then pass a lint-baseline.txt through to droidstubs.
1115 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1116 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1117 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1118 if err != nil {
1119 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1120 }
1121 if len(paths) == 1 {
1122 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1123 } else if len(paths) != 0 {
1124 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1125 }
Paul Duffin160fe412020-05-10 19:32:20 +01001126 }
1127 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001128
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001129 // Dist the api txt artifact for sdk builds.
1130 if !Bool(module.sdkLibraryProperties.No_dist) {
1131 props.Dist.Targets = []string{"sdk", "win_sdk"}
1132 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1133 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1134 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001135 }
1136
Colin Cross84dfc3d2019-09-25 11:33:01 -07001137 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001138}
1139
Jooyung Han5e9013b2020-03-10 06:23:13 +09001140func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1141 depTag := mctx.OtherModuleDependencyTag(dep)
1142 if depTag == xmlPermissionsFileTag {
1143 return true
1144 }
1145 return module.Library.DepIsInSameApex(mctx, dep)
1146}
1147
Jiyong Parkc678ad32018-04-10 13:07:10 +09001148// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001149func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001150 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001151 Name *string
1152 Lib_name *string
1153 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001154 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +09001155 Name: proptools.StringPtr(module.xmlFileName()),
1156 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1157 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001158 }
Jiyong Parke3833882020-02-17 17:28:10 +09001159
Jiyong Parke3833882020-02-17 17:28:10 +09001160 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001161}
1162
Paul Duffin50061512020-01-21 16:31:05 +00001163func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001164 var ver sdkVersion
1165 var kind sdkKind
1166 if s.usePrebuilt(ctx) {
1167 ver = s.version
1168 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001169 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001170 // We don't have prebuilt SDK for the specific sdkVersion.
1171 // Instead of breaking the build, fallback to use "system_current"
1172 ver = sdkVersionCurrent
1173 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001174 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001175
1176 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001177 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001178 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001179 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001180 if ctx.Config().AllowMissingDependencies() {
1181 return android.Paths{android.PathForSource(ctx, jar)}
1182 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001183 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001184 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001185 return nil
1186 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001187 return android.Paths{jarPath.Path()}
1188}
1189
Paul Duffinb05d4292020-05-20 12:19:10 +01001190func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001191
Paul Duffinb05d4292020-05-20 12:19:10 +01001192 // Check any special cases for java_sdk_library.
1193 if !sdkVersion.specified() {
Paul Duffind1b3a922020-01-22 11:57:20 +00001194 if headerJars {
Paul Duffinb05d4292020-05-20 12:19:10 +01001195 return module.HeaderJars()
Paul Duffind1b3a922020-01-22 11:57:20 +00001196 } else {
Paul Duffinb05d4292020-05-20 12:19:10 +01001197 return module.ImplementationJars()
Sundong Ahn054b19a2018-10-19 13:46:09 +09001198 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001199 } else if sdkVersion.kind == sdkPrivate {
1200 return module.HeaderJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +09001201 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001202
1203 return module.sdkJarsCommon(ctx, sdkVersion, headerJars)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001204}
1205
Sundong Ahn241cd372018-07-13 16:16:44 +09001206// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001207func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1208 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1209}
1210
1211// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001212func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001213 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001214}
1215
Sundong Ahn80a87b32019-05-13 15:02:50 +09001216func (module *SdkLibrary) SetNoDist() {
1217 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1218}
1219
Colin Cross571cccf2019-02-04 11:22:08 -08001220var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1221
Jiyong Park82484c02018-04-23 21:41:26 +09001222func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001223 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001224 return &[]string{}
1225 }).(*[]string)
1226}
1227
Paul Duffin749f98f2019-12-30 17:23:46 +00001228func (module *SdkLibrary) getApiDir() string {
1229 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1230}
1231
Jiyong Parkc678ad32018-04-10 13:07:10 +09001232// For a java_sdk_library module, create internal modules for stubs, docs,
1233// runtime libs and xml file. If requested, the stubs and docs are created twice
1234// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001235func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1236 // If the module has been disabled then don't create any child modules.
1237 if !module.Enabled() {
1238 return
1239 }
1240
Paul Duffina18abc22020-05-16 18:54:24 +01001241 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001242 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001243 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001244 }
1245
Paul Duffin37e0b772019-12-30 17:20:10 +00001246 // If this builds against standard libraries (i.e. is not part of the core libraries)
1247 // then assume it provides both system and test apis. Otherwise, assume it does not and
1248 // also assume it does not contribute to the dist build.
1249 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1250 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001251 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001252 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1253
Inseob Kim8098faa2019-03-18 10:19:51 +09001254 missing_current_api := false
1255
Paul Duffin3375e352020-04-28 10:44:03 +01001256 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001257
Paul Duffin749f98f2019-12-30 17:23:46 +00001258 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001259 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001260 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001261 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001262 p := android.ExistentPathForSource(mctx, path)
1263 if !p.Valid() {
1264 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1265 missing_current_api = true
1266 }
1267 }
1268 }
1269
1270 if missing_current_api {
1271 script := "build/soong/scripts/gen-java-current-api-files.sh"
1272 p := android.ExistentPathForSource(mctx, script)
1273
1274 if !p.Valid() {
1275 panic(fmt.Sprintf("script file %s doesn't exist", script))
1276 }
1277
1278 mctx.ModuleErrorf("One or more current api files are missing. "+
1279 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001280 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001281 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001282 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001283 return
1284 }
1285
Paul Duffin3375e352020-04-28 10:44:03 +01001286 for _, scope := range generatedScopes {
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001287 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinc3091c82020-05-08 14:16:20 +01001288 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001289
1290 // If the args needed to generate the stubs and API are the same then they
1291 // can be generated in a single invocation of metalava, otherwise they will
1292 // need separate invocations.
1293 if scope.createStubsSourceAndApiTogether {
1294 // Use the stubs source name for legacy reasons.
1295 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1296 } else {
1297 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1298
1299 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinc3091c82020-05-08 14:16:20 +01001300 apiName := module.apiModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001301 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1302 }
1303
Paul Duffind1b3a922020-01-22 11:57:20 +00001304 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001305 }
1306
Paul Duffin43db9be2019-12-30 17:35:49 +00001307 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
1308 // for runtime
1309 module.createXmlFile(mctx)
1310
1311 // record java_sdk_library modules so that they are exported to make
1312 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1313 javaSdkLibrariesLock.Lock()
1314 defer javaSdkLibrariesLock.Unlock()
1315 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1316 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001317}
1318
1319func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001320 module.AddProperties(
1321 &module.sdkLibraryProperties,
Paul Duffina18abc22020-05-16 18:54:24 +01001322 &module.properties,
1323 &module.dexpreoptProperties,
1324 &module.deviceProperties,
1325 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001326 )
1327
Paul Duffina18abc22020-05-16 18:54:24 +01001328 module.properties.Installable = proptools.BoolPtr(true)
1329 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001330}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001331
Paul Duffin1b1e8062020-05-08 13:44:43 +01001332// Defines how to name the individual component modules the sdk library creates.
1333type sdkLibraryComponentNamingScheme interface {
1334 stubsLibraryModuleName(scope *apiScope, baseName string) string
1335
1336 stubsSourceModuleName(scope *apiScope, baseName string) string
1337
1338 apiModuleName(scope *apiScope, baseName string) string
1339}
1340
1341type defaultNamingScheme struct {
1342}
1343
1344func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1345 return scope.stubsLibraryModuleName(baseName)
1346}
1347
1348func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1349 return scope.stubsSourceModuleName(baseName)
1350}
1351
1352func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1353 return scope.apiModuleName(baseName)
1354}
1355
1356var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1357
Paul Duffin6c9c5fc2020-05-08 15:36:30 +01001358type frameworkModulesNamingScheme struct {
1359}
1360
1361func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1362 suffix := scope.name
1363 if scope == apiScopeModuleLib {
1364 suffix = "module_libs_"
1365 }
1366 return suffix
1367}
1368
1369func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1370 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1371}
1372
1373func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1374 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1375}
1376
1377func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1378 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1379}
1380
1381var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1382
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001383// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1384// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1385// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1386// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1387// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001388func SdkLibraryFactory() android.Module {
1389 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001390
1391 // Initialize information common between source and prebuilt.
1392 module.initCommon(&module.ModuleBase)
1393
Inseob Kimc0907f12019-02-08 21:00:45 +09001394 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001395 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001396 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001397
1398 // Initialize the map from scope to scope specific properties.
1399 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1400 for _, scope := range allApiScopes {
1401 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1402 }
1403 module.scopeToProperties = scopeToProperties
1404
Paul Duffin4911a892020-04-29 23:35:13 +01001405 // Add the properties containing visibility rules so that they are checked.
1406 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1407 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1408
Paul Duffin1b1e8062020-05-08 13:44:43 +01001409 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
1410 if module.initCommonAfterDefaultsApplied(ctx) {
1411 module.CreateInternalModules(ctx)
1412 }
1413 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001414 return module
1415}
Colin Cross79c7c262019-04-17 11:11:46 -07001416
1417//
1418// SDK library prebuilts
1419//
1420
Paul Duffin56d44902020-01-31 13:36:25 +00001421// Properties associated with each api scope.
1422type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001423 Jars []string `android:"path"`
1424
1425 Sdk_version *string
1426
Colin Cross79c7c262019-04-17 11:11:46 -07001427 // List of shared java libs that this module has dependencies to
1428 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001429
Paul Duffinc8782502020-04-29 20:45:27 +01001430 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001431 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001432
1433 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001434 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001435
1436 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001437 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001438}
1439
Paul Duffin56d44902020-01-31 13:36:25 +00001440type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001441 // List of shared java libs, common to all scopes, that this module has
1442 // dependencies to
1443 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001444}
1445
Colin Cross79c7c262019-04-17 11:11:46 -07001446type sdkLibraryImport struct {
1447 android.ModuleBase
1448 android.DefaultableModuleBase
1449 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001450 android.ApexModuleBase
1451 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001452
1453 properties sdkLibraryImportProperties
1454
Paul Duffin46a26a82020-04-07 19:27:04 +01001455 // Map from api scope to the scope specific property structure.
1456 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1457
Paul Duffin56d44902020-01-31 13:36:25 +00001458 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001459}
1460
1461var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1462
Paul Duffin46a26a82020-04-07 19:27:04 +01001463// The type of a structure that contains a field of type sdkLibraryScopeProperties
1464// for each apiscope in allApiScopes, e.g. something like:
1465// struct {
1466// Public sdkLibraryScopeProperties
1467// System sdkLibraryScopeProperties
1468// ...
1469// }
1470var allScopeStructType = createAllScopePropertiesStructType()
1471
1472// Dynamically create a structure type for each apiscope in allApiScopes.
1473func createAllScopePropertiesStructType() reflect.Type {
1474 var fields []reflect.StructField
1475 for _, apiScope := range allApiScopes {
1476 field := reflect.StructField{
1477 Name: apiScope.fieldName,
1478 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1479 }
1480 fields = append(fields, field)
1481 }
1482
1483 return reflect.StructOf(fields)
1484}
1485
1486// Create an instance of the scope specific structure type and return a map
1487// from apiscope to a pointer to each scope specific field.
1488func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1489 allScopePropertiesPtr := reflect.New(allScopeStructType)
1490 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1491 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1492
1493 for _, apiScope := range allApiScopes {
1494 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1495 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1496 }
1497
1498 return allScopePropertiesPtr.Interface(), scopeProperties
1499}
1500
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001501// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001502func sdkLibraryImportFactory() android.Module {
1503 module := &sdkLibraryImport{}
1504
Paul Duffin46a26a82020-04-07 19:27:04 +01001505 allScopeProperties, scopeToProperties := createPropertiesInstance()
1506 module.scopeProperties = scopeToProperties
1507 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001508
Paul Duffinc3091c82020-05-08 14:16:20 +01001509 // Initialize information common between source and prebuilt.
1510 module.initCommon(&module.ModuleBase)
1511
Paul Duffin0bdcb272020-02-06 15:24:57 +00001512 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001513 android.InitApexModule(module)
1514 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001515 InitJavaModule(module, android.HostAndDeviceSupported)
1516
Paul Duffin1b1e8062020-05-08 13:44:43 +01001517 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1518 if module.initCommonAfterDefaultsApplied(mctx) {
1519 module.createInternalModules(mctx)
1520 }
1521 })
Colin Cross79c7c262019-04-17 11:11:46 -07001522 return module
1523}
1524
1525func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1526 return &module.prebuilt
1527}
1528
1529func (module *sdkLibraryImport) Name() string {
1530 return module.prebuilt.Name(module.ModuleBase.Name())
1531}
1532
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001533func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001534
Paul Duffin50061512020-01-21 16:31:05 +00001535 // If the build is configured to use prebuilts then force this to be preferred.
1536 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1537 module.prebuilt.ForcePrefer()
1538 }
1539
Paul Duffin46a26a82020-04-07 19:27:04 +01001540 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001541 if len(scopeProperties.Jars) == 0 {
1542 continue
1543 }
1544
Paul Duffinbbb546b2020-04-09 00:07:11 +01001545 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001546
Paul Duffin0f8faff2020-05-20 16:18:00 +01001547 if len(scopeProperties.Stub_srcs) > 0 {
1548 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1549 }
Paul Duffin56d44902020-01-31 13:36:25 +00001550 }
Colin Cross79c7c262019-04-17 11:11:46 -07001551
1552 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1553 javaSdkLibrariesLock.Lock()
1554 defer javaSdkLibrariesLock.Unlock()
1555 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1556}
1557
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001558func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001559 // Creates a java import for the jar with ".stubs" suffix
1560 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001561 Name *string
1562 Sdk_version *string
1563 Libs []string
1564 Jars []string
1565 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001566 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001567 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001568 props.Sdk_version = scopeProperties.Sdk_version
1569 // Prepend any of the libs from the legacy public properties to the libs for each of the
1570 // scopes to avoid having to duplicate them in each scope.
1571 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1572 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001573
Paul Duffin38b57852020-05-13 16:08:09 +01001574 // The imports are preferred if the java_sdk_library_import is preferred.
1575 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001576 mctx.CreateModule(ImportFactory, &props)
1577}
1578
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001579func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001580 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001581 Name *string
1582 Srcs []string
1583 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001584 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001585 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001586 props.Srcs = scopeProperties.Stub_srcs
1587 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001588
1589 // The stubs source is preferred if the java_sdk_library_import is preferred.
1590 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001591}
1592
Colin Cross79c7c262019-04-17 11:11:46 -07001593func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001594 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001595 if len(scopeProperties.Jars) == 0 {
1596 continue
1597 }
1598
1599 // Add dependencies to the prebuilt stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001600 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001601
1602 if len(scopeProperties.Stub_srcs) > 0 {
1603 // Add dependencies to the prebuilt stubs source library
1604 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1605 }
Paul Duffin56d44902020-01-31 13:36:25 +00001606 }
Colin Cross79c7c262019-04-17 11:11:46 -07001607}
1608
Paul Duffin46dc45a2020-05-14 15:39:10 +01001609func (module *sdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
1610 return module.commonOutputFiles(tag)
1611}
1612
Colin Cross79c7c262019-04-17 11:11:46 -07001613func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin0f8faff2020-05-20 16:18:00 +01001614 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001615 ctx.VisitDirectDeps(func(to android.Module) {
1616 tag := ctx.OtherModuleDependencyTag(to)
1617
Paul Duffin0f8faff2020-05-20 16:18:00 +01001618 // Extract information from any of the scope specific dependencies.
1619 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1620 apiScope := scopeTag.apiScope
1621 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1622
1623 // Extract information from the dependency. The exact information extracted
1624 // is determined by the nature of the dependency which is determined by the tag.
1625 scopeTag.extractDepInfo(ctx, to, scopePaths)
Colin Cross79c7c262019-04-17 11:11:46 -07001626 }
1627 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001628
1629 // Populate the scope paths with information from the properties.
1630 for apiScope, scopeProperties := range module.scopeProperties {
1631 if len(scopeProperties.Jars) == 0 {
1632 continue
1633 }
1634
1635 paths := module.getScopePathsCreateIfNeeded(apiScope)
1636 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1637 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1638 }
Colin Cross79c7c262019-04-17 11:11:46 -07001639}
1640
Paul Duffinb05d4292020-05-20 12:19:10 +01001641func (module *sdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin56d44902020-01-31 13:36:25 +00001642
Paul Duffinb05d4292020-05-20 12:19:10 +01001643 // The java_sdk_library_import can only ever give back header jars as it does not
1644 // have an implementation jar.
1645 headerJars := true
1646 return module.sdkJarsCommon(ctx, sdkVersion, headerJars)
Paul Duffin56d44902020-01-31 13:36:25 +00001647}
1648
Colin Cross79c7c262019-04-17 11:11:46 -07001649// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001650func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001651 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001652 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001653}
1654
1655// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001656func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001657 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001658 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001659}
Jiyong Parke3833882020-02-17 17:28:10 +09001660
1661//
1662// java_sdk_library_xml
1663//
1664type sdkLibraryXml struct {
1665 android.ModuleBase
1666 android.DefaultableModuleBase
1667 android.ApexModuleBase
1668
1669 properties sdkLibraryXmlProperties
1670
1671 outputFilePath android.OutputPath
1672 installDirPath android.InstallPath
1673}
1674
1675type sdkLibraryXmlProperties struct {
1676 // canonical name of the lib
1677 Lib_name *string
1678}
1679
1680// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1681// Not to be used directly by users. java_sdk_library internally uses this.
1682func sdkLibraryXmlFactory() android.Module {
1683 module := &sdkLibraryXml{}
1684
1685 module.AddProperties(&module.properties)
1686
1687 android.InitApexModule(module)
1688 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1689
1690 return module
1691}
1692
1693// from android.PrebuiltEtcModule
1694func (module *sdkLibraryXml) SubDir() string {
1695 return "permissions"
1696}
1697
1698// from android.PrebuiltEtcModule
1699func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1700 return module.outputFilePath
1701}
1702
1703// from android.ApexModule
1704func (module *sdkLibraryXml) AvailableFor(what string) bool {
1705 return true
1706}
1707
1708func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1709 // do nothing
1710}
1711
1712// File path to the runtime implementation library
1713func (module *sdkLibraryXml) implPath() string {
1714 implName := proptools.String(module.properties.Lib_name)
1715 if apexName := module.ApexName(); apexName != "" {
1716 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1717 // In most cases, this works fine. But when apex_name is set or override_apex is used
1718 // this can be wrong.
1719 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1720 }
1721 partition := "system"
1722 if module.SocSpecific() {
1723 partition = "vendor"
1724 } else if module.DeviceSpecific() {
1725 partition = "odm"
1726 } else if module.ProductSpecific() {
1727 partition = "product"
1728 } else if module.SystemExtSpecific() {
1729 partition = "system_ext"
1730 }
1731 return "/" + partition + "/framework/" + implName + ".jar"
1732}
1733
1734func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1735 libName := proptools.String(module.properties.Lib_name)
1736 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1737
1738 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1739 rule := android.NewRuleBuilder()
1740 rule.Command().
1741 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1742 Output(module.outputFilePath)
1743
1744 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1745
1746 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1747}
1748
1749func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1750 if !module.IsForPlatform() {
1751 return []android.AndroidMkEntries{android.AndroidMkEntries{
1752 Disabled: true,
1753 }}
1754 }
1755
1756 return []android.AndroidMkEntries{android.AndroidMkEntries{
1757 Class: "ETC",
1758 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1759 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1760 func(entries *android.AndroidMkEntries) {
1761 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1762 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1763 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1764 },
1765 },
1766 }}
1767}
Paul Duffindd46f712020-02-10 13:37:10 +00001768
1769type sdkLibrarySdkMemberType struct {
1770 android.SdkMemberTypeBase
1771}
1772
1773func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1774 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1775}
1776
1777func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1778 _, ok := module.(*SdkLibrary)
1779 return ok
1780}
1781
1782func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1783 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1784}
1785
1786func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1787 return &sdkLibrarySdkMemberProperties{}
1788}
1789
1790type sdkLibrarySdkMemberProperties struct {
1791 android.SdkMemberPropertiesBase
1792
1793 // Scope to per scope properties.
1794 Scopes map[*apiScope]scopeProperties
1795
1796 // Additional libraries that the exported stubs libraries depend upon.
1797 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001798
1799 // The Java stubs source files.
1800 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01001801
1802 // The naming scheme.
1803 Naming_scheme *string
Paul Duffindd46f712020-02-10 13:37:10 +00001804}
1805
1806type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001807 Jars android.Paths
1808 StubsSrcJar android.Path
1809 CurrentApiFile android.Path
1810 RemovedApiFile android.Path
1811 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001812}
1813
1814func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1815 sdk := variant.(*SdkLibrary)
1816
1817 s.Scopes = make(map[*apiScope]scopeProperties)
1818 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001819 paths := sdk.findScopePaths(apiScope)
1820 if paths == nil {
1821 continue
1822 }
1823
Paul Duffindd46f712020-02-10 13:37:10 +00001824 jars := paths.stubsImplPath
1825 if len(jars) > 0 {
1826 properties := scopeProperties{}
1827 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01001828 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01001829 properties.StubsSrcJar = paths.stubsSrcJar.Path()
1830 properties.CurrentApiFile = paths.currentApiFilePath.Path()
1831 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffindd46f712020-02-10 13:37:10 +00001832 s.Scopes[apiScope] = properties
1833 }
1834 }
1835
1836 s.Libs = sdk.properties.Libs
Paul Duffinf7a64332020-05-13 16:54:55 +01001837 s.Naming_scheme = sdk.commonProperties.Naming_scheme
Paul Duffindd46f712020-02-10 13:37:10 +00001838}
1839
1840func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01001841 if s.Naming_scheme != nil {
1842 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
1843 }
1844
Paul Duffindd46f712020-02-10 13:37:10 +00001845 for _, apiScope := range allApiScopes {
1846 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01001847 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00001848
Paul Duffin3d1248c2020-04-09 00:10:17 +01001849 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1850
Paul Duffindd46f712020-02-10 13:37:10 +00001851 var jars []string
1852 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001853 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001854 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1855 jars = append(jars, dest)
1856 }
1857 scopeSet.AddProperty("jars", jars)
1858
Paul Duffin3d1248c2020-04-09 00:10:17 +01001859 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1860 // the source files are also unpacked.
1861 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1862 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1863 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1864
Paul Duffin1fd005d2020-04-09 01:08:11 +01001865 if properties.CurrentApiFile != nil {
1866 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1867 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1868 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1869 }
1870
1871 if properties.RemovedApiFile != nil {
1872 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1873 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1874 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1875 }
1876
Paul Duffindd46f712020-02-10 13:37:10 +00001877 if properties.SdkVersion != "" {
1878 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1879 }
1880 }
1881 }
1882
1883 if len(s.Libs) > 0 {
1884 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1885 }
1886}