blob: 5efb4d04540092d97d3ccb4258c6e544cf31c128 [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 Duffin23970f42020-05-20 14:20:02 +0100697func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100698
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 Duffin23970f42020-05-20 14:20:02 +0100728 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100729}
730
Inseob Kimc0907f12019-02-08 21:00:45 +0900731type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900732 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900733
Sundong Ahn054b19a2018-10-19 13:46:09 +0900734 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900735
Paul Duffin3375e352020-04-28 10:44:03 +0100736 // Map from api scope to the scope specific property structure.
737 scopeToProperties map[*apiScope]*ApiScopeProperties
738
Paul Duffin56d44902020-01-31 13:36:25 +0000739 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900740}
741
Inseob Kimc0907f12019-02-08 21:00:45 +0900742var _ Dependency = (*SdkLibrary)(nil)
743var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800744
Paul Duffin3375e352020-04-28 10:44:03 +0100745func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
746 return module.sdkLibraryProperties.Generate_system_and_test_apis
747}
748
749func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
750 // Check to see if any scopes have been explicitly enabled. If any have then all
751 // must be.
752 anyScopesExplicitlyEnabled := false
753 for _, scope := range allApiScopes {
754 scopeProperties := module.scopeToProperties[scope]
755 if scopeProperties.Enabled != nil {
756 anyScopesExplicitlyEnabled = true
757 break
758 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000759 }
Paul Duffin3375e352020-04-28 10:44:03 +0100760
761 var generatedScopes apiScopes
762 enabledScopes := make(map[*apiScope]struct{})
763 for _, scope := range allApiScopes {
764 scopeProperties := module.scopeToProperties[scope]
765 // If any scopes are explicitly enabled then ignore the legacy enabled status.
766 // This is to ensure that any new usages of this module type do not rely on legacy
767 // behaviour.
768 defaultEnabledStatus := false
769 if anyScopesExplicitlyEnabled {
770 defaultEnabledStatus = scope.defaultEnabledStatus
771 } else {
772 defaultEnabledStatus = scope.legacyEnabledStatus(module)
773 }
774 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
775 if enabled {
776 enabledScopes[scope] = struct{}{}
777 generatedScopes = append(generatedScopes, scope)
778 }
779 }
780
781 // Now check to make sure that any scope that is extended by an enabled scope is also
782 // enabled.
783 for _, scope := range allApiScopes {
784 if _, ok := enabledScopes[scope]; ok {
785 extends := scope.extends
786 if extends != nil {
787 if _, ok := enabledScopes[extends]; !ok {
788 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
789 }
790 }
791 }
792 }
793
794 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000795}
796
Paul Duffine74ac732020-02-06 13:51:46 +0000797var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
798
Jiyong Parke3833882020-02-17 17:28:10 +0900799func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
800 if dt, ok := depTag.(dependencyTag); ok {
801 return dt == xmlPermissionsFileTag
802 }
803 return false
804}
805
Inseob Kimc0907f12019-02-08 21:00:45 +0900806func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100807 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000808 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100809 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000810
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100811 // If the stubs source and API cannot be generated together then add an additional dependency on
812 // the API module.
813 if apiScope.createStubsSourceAndApiTogether {
814 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinc3091c82020-05-08 14:16:20 +0100815 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100816 } else {
817 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinc3091c82020-05-08 14:16:20 +0100818 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
819 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100820 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900821 }
822
Paul Duffine74ac732020-02-06 13:51:46 +0000823 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
824 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900825 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000826 }
827
Sundong Ahn054b19a2018-10-19 13:46:09 +0900828 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900829}
830
Paul Duffin46dc45a2020-05-14 15:39:10 +0100831func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
832 paths, err := module.commonOutputFiles(tag)
833 if paths == nil && err == nil {
834 return module.Library.OutputFiles(tag)
835 } else {
836 return paths, err
837 }
838}
839
Inseob Kimc0907f12019-02-08 21:00:45 +0900840func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000841 // Don't build an implementation library if this is api only.
842 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
843 module.Library.GenerateAndroidBuildActions(ctx)
844 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900845
Sundong Ahn57368eb2018-07-06 11:20:23 +0900846 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000847 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900848 // the recorded paths will be returned depending on the link type of the caller.
849 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900850 tag := ctx.OtherModuleDependencyTag(to)
851
Paul Duffinc8782502020-04-29 20:45:27 +0100852 // Extract information from any of the scope specific dependencies.
853 if scopeTag, ok := tag.(scopeDependencyTag); ok {
854 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +0100855 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +0100856
857 // Extract information from the dependency. The exact information extracted
858 // is determined by the nature of the dependency which is determined by the tag.
859 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900860 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900861 })
862}
863
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900864func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000865 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
866 return nil
867 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900868 entriesList := module.Library.AndroidMkEntries()
869 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700870 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900871 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900872}
873
Jiyong Parkc678ad32018-04-10 13:07:10 +0900874// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900875func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900876 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900877}
878
Jiyong Parkc678ad32018-04-10 13:07:10 +0900879// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900880func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900881 return module.BaseModuleName() + sdkXmlFileSuffix
882}
883
Anton Hansson5fd5d242020-03-27 19:43:19 +0000884// The dist path of the stub artifacts
885func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
886 if module.ModuleBase.Owner() != "" {
887 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
888 } else if Bool(module.sdkLibraryProperties.Core_lib) {
889 return path.Join("apistubs", "core", apiScope.name)
890 } else {
891 return path.Join("apistubs", "android", apiScope.name)
892 }
893}
894
Paul Duffin12ceb462019-12-24 20:31:31 +0000895// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +0100896func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +0100897 scopeProperties := module.scopeToProperties[apiScope]
898 if scopeProperties.Sdk_version != nil {
899 return proptools.String(scopeProperties.Sdk_version)
900 }
901
Paul Duffin12ceb462019-12-24 20:31:31 +0000902 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
903 if sdkDep.hasStandardLibs() {
904 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000905 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000906 } else {
907 // Otherwise, use no system module.
908 return "none"
909 }
910}
911
Paul Duffind1b3a922020-01-22 11:57:20 +0000912func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
913 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900914}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900915
Paul Duffind1b3a922020-01-22 11:57:20 +0000916func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
917 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900918}
919
920// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100921func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900922 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +0100923 Name *string
924 Visibility []string
925 Srcs []string
926 Installable *bool
927 Sdk_version *string
928 System_modules *string
929 Patch_module *string
930 Libs []string
931 Compile_dex *bool
932 Java_version *string
933 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900934 Pdk struct {
935 Enabled *bool
936 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900937 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900938 Openjdk9 struct {
939 Srcs []string
940 Javacflags []string
941 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000942 Dist struct {
943 Targets []string
944 Dest *string
945 Dir *string
946 Tag *string
947 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900948 }{}
949
Paul Duffinc3091c82020-05-08 14:16:20 +0100950 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100951
952 // If stubs_library_visibility is not set then the created module will use the
953 // visibility of this module.
954 visibility := module.sdkLibraryProperties.Stubs_library_visibility
955 props.Visibility = visibility
956
Jiyong Parkc678ad32018-04-10 13:07:10 +0900957 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +0100958 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000959 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100960 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +0100961 props.System_modules = module.deviceProperties.System_modules
962 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000963 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900964 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900965 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffina18abc22020-05-16 18:54:24 +0100966 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
967 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
968 props.Java_version = module.properties.Java_version
969 if module.deviceProperties.Compile_dex != nil {
970 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900971 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900972
Anton Hansson5fd5d242020-03-27 19:43:19 +0000973 // Dist the class jar artifact for sdk builds.
974 if !Bool(module.sdkLibraryProperties.No_dist) {
975 props.Dist.Targets = []string{"sdk", "win_sdk"}
976 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
977 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
978 props.Dist.Tag = proptools.StringPtr(".jar")
979 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900980
Colin Cross84dfc3d2019-09-25 11:33:01 -0700981 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900982}
983
Paul Duffin6d0886e2020-04-07 18:49:53 +0100984// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +0100985// files and also updates and checks the API specification files.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100986func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900987 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900988 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100989 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900990 Srcs []string
991 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100992 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000993 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900994 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000995 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900996 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900997 Java_version *string
998 Merge_annotations_dirs []string
999 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001000 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001001 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001002 Current ApiToCheck
1003 Last_released ApiToCheck
1004 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001005
1006 Api_lint struct {
1007 Enabled *bool
1008 New_since *string
1009 Baseline_file *string
1010 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001011 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001012 Aidl struct {
1013 Include_dirs []string
1014 Local_include_dirs []string
1015 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001016 Dist struct {
1017 Targets []string
1018 Dest *string
1019 Dir *string
1020 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001021 }{}
1022
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001023 // The stubs source processing uses the same compile time classpath when extracting the
1024 // API from the implementation library as it does when compiling it. i.e. the same
1025 // * sdk version
1026 // * system_modules
1027 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001028
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001029 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +01001030
1031 // If stubs_source_visibility is not set then the created module will use the
1032 // visibility of this module.
1033 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1034 props.Visibility = visibility
1035
Paul Duffina18abc22020-05-16 18:54:24 +01001036 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1037 props.Sdk_version = module.deviceProperties.Sdk_version
1038 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001039 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001040 // A droiddoc module has only one Libs property and doesn't distinguish between
1041 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001042 props.Libs = module.properties.Libs
1043 props.Libs = append(props.Libs, module.properties.Static_libs...)
1044 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1045 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1046 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001047
Sundong Ahn054b19a2018-10-19 13:46:09 +09001048 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1049 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1050
Paul Duffin6d0886e2020-04-07 18:49:53 +01001051 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001052 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001053 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001054 }
1055 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001056 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001057 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1058 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001059 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001060 disabledWarnings := []string{
1061 "MissingPermission",
1062 "BroadcastBehavior",
1063 "HiddenSuperclass",
1064 "DeprecationMismatch",
1065 "UnavailableSymbol",
1066 "SdkConstant",
1067 "HiddenTypeParameter",
1068 "Todo",
1069 "Typo",
1070 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001071 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001072
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001073 if !createStubSources {
1074 // Stubs are not required.
1075 props.Generate_stubs = proptools.BoolPtr(false)
1076 }
1077
Paul Duffin1fb487d2020-04-07 18:50:10 +01001078 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001079 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001080 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001081 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001082
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001083 if createApi {
1084 // List of APIs identified from the provided source files are created. They are later
1085 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1086 // last-released (a.k.a numbered) list of API.
1087 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1088 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1089 apiDir := module.getApiDir()
1090 currentApiFileName = path.Join(apiDir, currentApiFileName)
1091 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001092
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001093 // check against the not-yet-release API
1094 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1095 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001096
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001097 if !apiScope.unstable {
1098 // check against the latest released API
1099 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1100 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1101 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1102 module.latestRemovedApiFilegroupName(apiScope))
1103 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001104
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001105 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1106 // Enable api lint.
1107 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1108 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001109
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001110 // If it exists then pass a lint-baseline.txt through to droidstubs.
1111 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1112 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1113 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1114 if err != nil {
1115 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1116 }
1117 if len(paths) == 1 {
1118 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1119 } else if len(paths) != 0 {
1120 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1121 }
Paul Duffin160fe412020-05-10 19:32:20 +01001122 }
1123 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001124
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001125 // Dist the api txt artifact for sdk builds.
1126 if !Bool(module.sdkLibraryProperties.No_dist) {
1127 props.Dist.Targets = []string{"sdk", "win_sdk"}
1128 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1129 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1130 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001131 }
1132
Colin Cross84dfc3d2019-09-25 11:33:01 -07001133 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001134}
1135
Jooyung Han5e9013b2020-03-10 06:23:13 +09001136func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1137 depTag := mctx.OtherModuleDependencyTag(dep)
1138 if depTag == xmlPermissionsFileTag {
1139 return true
1140 }
1141 return module.Library.DepIsInSameApex(mctx, dep)
1142}
1143
Jiyong Parkc678ad32018-04-10 13:07:10 +09001144// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001145func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001146 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001147 Name *string
1148 Lib_name *string
1149 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001150 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +09001151 Name: proptools.StringPtr(module.xmlFileName()),
1152 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1153 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001154 }
Jiyong Parke3833882020-02-17 17:28:10 +09001155
Jiyong Parke3833882020-02-17 17:28:10 +09001156 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001157}
1158
Paul Duffin50061512020-01-21 16:31:05 +00001159func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001160 var ver sdkVersion
1161 var kind sdkKind
1162 if s.usePrebuilt(ctx) {
1163 ver = s.version
1164 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001165 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001166 // We don't have prebuilt SDK for the specific sdkVersion.
1167 // Instead of breaking the build, fallback to use "system_current"
1168 ver = sdkVersionCurrent
1169 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001170 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001171
1172 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001173 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001174 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001175 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001176 if ctx.Config().AllowMissingDependencies() {
1177 return android.Paths{android.PathForSource(ctx, jar)}
1178 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001179 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001180 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001181 return nil
1182 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001183 return android.Paths{jarPath.Path()}
1184}
1185
Paul Duffinb05d4292020-05-20 12:19:10 +01001186func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001187
Paul Duffinb05d4292020-05-20 12:19:10 +01001188 // Check any special cases for java_sdk_library.
1189 if !sdkVersion.specified() {
Paul Duffind1b3a922020-01-22 11:57:20 +00001190 if headerJars {
Paul Duffinb05d4292020-05-20 12:19:10 +01001191 return module.HeaderJars()
Paul Duffind1b3a922020-01-22 11:57:20 +00001192 } else {
Paul Duffinb05d4292020-05-20 12:19:10 +01001193 return module.ImplementationJars()
Sundong Ahn054b19a2018-10-19 13:46:09 +09001194 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001195 } else if sdkVersion.kind == sdkPrivate {
1196 return module.HeaderJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +09001197 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001198
Paul Duffin23970f42020-05-20 14:20:02 +01001199 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001200}
1201
Sundong Ahn241cd372018-07-13 16:16:44 +09001202// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001203func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1204 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1205}
1206
1207// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001208func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001209 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001210}
1211
Sundong Ahn80a87b32019-05-13 15:02:50 +09001212func (module *SdkLibrary) SetNoDist() {
1213 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1214}
1215
Colin Cross571cccf2019-02-04 11:22:08 -08001216var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1217
Jiyong Park82484c02018-04-23 21:41:26 +09001218func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001219 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001220 return &[]string{}
1221 }).(*[]string)
1222}
1223
Paul Duffin749f98f2019-12-30 17:23:46 +00001224func (module *SdkLibrary) getApiDir() string {
1225 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1226}
1227
Jiyong Parkc678ad32018-04-10 13:07:10 +09001228// For a java_sdk_library module, create internal modules for stubs, docs,
1229// runtime libs and xml file. If requested, the stubs and docs are created twice
1230// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001231func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1232 // If the module has been disabled then don't create any child modules.
1233 if !module.Enabled() {
1234 return
1235 }
1236
Paul Duffina18abc22020-05-16 18:54:24 +01001237 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001238 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001239 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001240 }
1241
Paul Duffin37e0b772019-12-30 17:20:10 +00001242 // If this builds against standard libraries (i.e. is not part of the core libraries)
1243 // then assume it provides both system and test apis. Otherwise, assume it does not and
1244 // also assume it does not contribute to the dist build.
1245 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1246 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001247 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001248 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1249
Inseob Kim8098faa2019-03-18 10:19:51 +09001250 missing_current_api := false
1251
Paul Duffin3375e352020-04-28 10:44:03 +01001252 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001253
Paul Duffin749f98f2019-12-30 17:23:46 +00001254 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001255 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001256 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001257 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001258 p := android.ExistentPathForSource(mctx, path)
1259 if !p.Valid() {
1260 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1261 missing_current_api = true
1262 }
1263 }
1264 }
1265
1266 if missing_current_api {
1267 script := "build/soong/scripts/gen-java-current-api-files.sh"
1268 p := android.ExistentPathForSource(mctx, script)
1269
1270 if !p.Valid() {
1271 panic(fmt.Sprintf("script file %s doesn't exist", script))
1272 }
1273
1274 mctx.ModuleErrorf("One or more current api files are missing. "+
1275 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001276 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001277 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001278 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001279 return
1280 }
1281
Paul Duffin3375e352020-04-28 10:44:03 +01001282 for _, scope := range generatedScopes {
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001283 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinc3091c82020-05-08 14:16:20 +01001284 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001285
1286 // If the args needed to generate the stubs and API are the same then they
1287 // can be generated in a single invocation of metalava, otherwise they will
1288 // need separate invocations.
1289 if scope.createStubsSourceAndApiTogether {
1290 // Use the stubs source name for legacy reasons.
1291 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1292 } else {
1293 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1294
1295 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinc3091c82020-05-08 14:16:20 +01001296 apiName := module.apiModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001297 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1298 }
1299
Paul Duffind1b3a922020-01-22 11:57:20 +00001300 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001301 }
1302
Paul Duffin43db9be2019-12-30 17:35:49 +00001303 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
1304 // for runtime
1305 module.createXmlFile(mctx)
1306
1307 // record java_sdk_library modules so that they are exported to make
1308 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1309 javaSdkLibrariesLock.Lock()
1310 defer javaSdkLibrariesLock.Unlock()
1311 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1312 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001313}
1314
1315func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001316 module.AddProperties(
1317 &module.sdkLibraryProperties,
Paul Duffina18abc22020-05-16 18:54:24 +01001318 &module.properties,
1319 &module.dexpreoptProperties,
1320 &module.deviceProperties,
1321 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001322 )
1323
Paul Duffina18abc22020-05-16 18:54:24 +01001324 module.properties.Installable = proptools.BoolPtr(true)
1325 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001326}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001327
Paul Duffin1b1e8062020-05-08 13:44:43 +01001328// Defines how to name the individual component modules the sdk library creates.
1329type sdkLibraryComponentNamingScheme interface {
1330 stubsLibraryModuleName(scope *apiScope, baseName string) string
1331
1332 stubsSourceModuleName(scope *apiScope, baseName string) string
1333
1334 apiModuleName(scope *apiScope, baseName string) string
1335}
1336
1337type defaultNamingScheme struct {
1338}
1339
1340func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1341 return scope.stubsLibraryModuleName(baseName)
1342}
1343
1344func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1345 return scope.stubsSourceModuleName(baseName)
1346}
1347
1348func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1349 return scope.apiModuleName(baseName)
1350}
1351
1352var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1353
Paul Duffin6c9c5fc2020-05-08 15:36:30 +01001354type frameworkModulesNamingScheme struct {
1355}
1356
1357func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1358 suffix := scope.name
1359 if scope == apiScopeModuleLib {
1360 suffix = "module_libs_"
1361 }
1362 return suffix
1363}
1364
1365func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1366 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1367}
1368
1369func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1370 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1371}
1372
1373func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1374 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1375}
1376
1377var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1378
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001379// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1380// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1381// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1382// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1383// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001384func SdkLibraryFactory() android.Module {
1385 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001386
1387 // Initialize information common between source and prebuilt.
1388 module.initCommon(&module.ModuleBase)
1389
Inseob Kimc0907f12019-02-08 21:00:45 +09001390 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001391 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001392 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001393
1394 // Initialize the map from scope to scope specific properties.
1395 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1396 for _, scope := range allApiScopes {
1397 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1398 }
1399 module.scopeToProperties = scopeToProperties
1400
Paul Duffin4911a892020-04-29 23:35:13 +01001401 // Add the properties containing visibility rules so that they are checked.
1402 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1403 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1404
Paul Duffin1b1e8062020-05-08 13:44:43 +01001405 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
1406 if module.initCommonAfterDefaultsApplied(ctx) {
1407 module.CreateInternalModules(ctx)
1408 }
1409 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001410 return module
1411}
Colin Cross79c7c262019-04-17 11:11:46 -07001412
1413//
1414// SDK library prebuilts
1415//
1416
Paul Duffin56d44902020-01-31 13:36:25 +00001417// Properties associated with each api scope.
1418type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001419 Jars []string `android:"path"`
1420
1421 Sdk_version *string
1422
Colin Cross79c7c262019-04-17 11:11:46 -07001423 // List of shared java libs that this module has dependencies to
1424 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001425
Paul Duffinc8782502020-04-29 20:45:27 +01001426 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001427 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001428
1429 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001430 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001431
1432 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001433 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001434}
1435
Paul Duffin56d44902020-01-31 13:36:25 +00001436type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001437 // List of shared java libs, common to all scopes, that this module has
1438 // dependencies to
1439 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001440}
1441
Colin Cross79c7c262019-04-17 11:11:46 -07001442type sdkLibraryImport struct {
1443 android.ModuleBase
1444 android.DefaultableModuleBase
1445 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001446 android.ApexModuleBase
1447 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001448
1449 properties sdkLibraryImportProperties
1450
Paul Duffin46a26a82020-04-07 19:27:04 +01001451 // Map from api scope to the scope specific property structure.
1452 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1453
Paul Duffin56d44902020-01-31 13:36:25 +00001454 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001455}
1456
1457var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1458
Paul Duffin46a26a82020-04-07 19:27:04 +01001459// The type of a structure that contains a field of type sdkLibraryScopeProperties
1460// for each apiscope in allApiScopes, e.g. something like:
1461// struct {
1462// Public sdkLibraryScopeProperties
1463// System sdkLibraryScopeProperties
1464// ...
1465// }
1466var allScopeStructType = createAllScopePropertiesStructType()
1467
1468// Dynamically create a structure type for each apiscope in allApiScopes.
1469func createAllScopePropertiesStructType() reflect.Type {
1470 var fields []reflect.StructField
1471 for _, apiScope := range allApiScopes {
1472 field := reflect.StructField{
1473 Name: apiScope.fieldName,
1474 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1475 }
1476 fields = append(fields, field)
1477 }
1478
1479 return reflect.StructOf(fields)
1480}
1481
1482// Create an instance of the scope specific structure type and return a map
1483// from apiscope to a pointer to each scope specific field.
1484func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1485 allScopePropertiesPtr := reflect.New(allScopeStructType)
1486 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1487 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1488
1489 for _, apiScope := range allApiScopes {
1490 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1491 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1492 }
1493
1494 return allScopePropertiesPtr.Interface(), scopeProperties
1495}
1496
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001497// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001498func sdkLibraryImportFactory() android.Module {
1499 module := &sdkLibraryImport{}
1500
Paul Duffin46a26a82020-04-07 19:27:04 +01001501 allScopeProperties, scopeToProperties := createPropertiesInstance()
1502 module.scopeProperties = scopeToProperties
1503 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001504
Paul Duffinc3091c82020-05-08 14:16:20 +01001505 // Initialize information common between source and prebuilt.
1506 module.initCommon(&module.ModuleBase)
1507
Paul Duffin0bdcb272020-02-06 15:24:57 +00001508 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001509 android.InitApexModule(module)
1510 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001511 InitJavaModule(module, android.HostAndDeviceSupported)
1512
Paul Duffin1b1e8062020-05-08 13:44:43 +01001513 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1514 if module.initCommonAfterDefaultsApplied(mctx) {
1515 module.createInternalModules(mctx)
1516 }
1517 })
Colin Cross79c7c262019-04-17 11:11:46 -07001518 return module
1519}
1520
1521func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1522 return &module.prebuilt
1523}
1524
1525func (module *sdkLibraryImport) Name() string {
1526 return module.prebuilt.Name(module.ModuleBase.Name())
1527}
1528
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001529func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001530
Paul Duffin50061512020-01-21 16:31:05 +00001531 // If the build is configured to use prebuilts then force this to be preferred.
1532 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1533 module.prebuilt.ForcePrefer()
1534 }
1535
Paul Duffin46a26a82020-04-07 19:27:04 +01001536 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001537 if len(scopeProperties.Jars) == 0 {
1538 continue
1539 }
1540
Paul Duffinbbb546b2020-04-09 00:07:11 +01001541 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001542
Paul Duffin0f8faff2020-05-20 16:18:00 +01001543 if len(scopeProperties.Stub_srcs) > 0 {
1544 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1545 }
Paul Duffin56d44902020-01-31 13:36:25 +00001546 }
Colin Cross79c7c262019-04-17 11:11:46 -07001547
1548 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1549 javaSdkLibrariesLock.Lock()
1550 defer javaSdkLibrariesLock.Unlock()
1551 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1552}
1553
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001554func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001555 // Creates a java import for the jar with ".stubs" suffix
1556 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001557 Name *string
1558 Sdk_version *string
1559 Libs []string
1560 Jars []string
1561 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001562 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001563 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001564 props.Sdk_version = scopeProperties.Sdk_version
1565 // Prepend any of the libs from the legacy public properties to the libs for each of the
1566 // scopes to avoid having to duplicate them in each scope.
1567 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1568 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001569
Paul Duffin38b57852020-05-13 16:08:09 +01001570 // The imports are preferred if the java_sdk_library_import is preferred.
1571 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001572 mctx.CreateModule(ImportFactory, &props)
1573}
1574
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001575func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001576 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001577 Name *string
1578 Srcs []string
1579 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001580 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001581 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001582 props.Srcs = scopeProperties.Stub_srcs
1583 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001584
1585 // The stubs source is preferred if the java_sdk_library_import is preferred.
1586 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001587}
1588
Colin Cross79c7c262019-04-17 11:11:46 -07001589func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001590 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001591 if len(scopeProperties.Jars) == 0 {
1592 continue
1593 }
1594
1595 // Add dependencies to the prebuilt stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001596 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001597
1598 if len(scopeProperties.Stub_srcs) > 0 {
1599 // Add dependencies to the prebuilt stubs source library
1600 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1601 }
Paul Duffin56d44902020-01-31 13:36:25 +00001602 }
Colin Cross79c7c262019-04-17 11:11:46 -07001603}
1604
Paul Duffin46dc45a2020-05-14 15:39:10 +01001605func (module *sdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
1606 return module.commonOutputFiles(tag)
1607}
1608
Colin Cross79c7c262019-04-17 11:11:46 -07001609func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin0f8faff2020-05-20 16:18:00 +01001610 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001611 ctx.VisitDirectDeps(func(to android.Module) {
1612 tag := ctx.OtherModuleDependencyTag(to)
1613
Paul Duffin0f8faff2020-05-20 16:18:00 +01001614 // Extract information from any of the scope specific dependencies.
1615 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1616 apiScope := scopeTag.apiScope
1617 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1618
1619 // Extract information from the dependency. The exact information extracted
1620 // is determined by the nature of the dependency which is determined by the tag.
1621 scopeTag.extractDepInfo(ctx, to, scopePaths)
Colin Cross79c7c262019-04-17 11:11:46 -07001622 }
1623 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001624
1625 // Populate the scope paths with information from the properties.
1626 for apiScope, scopeProperties := range module.scopeProperties {
1627 if len(scopeProperties.Jars) == 0 {
1628 continue
1629 }
1630
1631 paths := module.getScopePathsCreateIfNeeded(apiScope)
1632 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1633 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1634 }
Colin Cross79c7c262019-04-17 11:11:46 -07001635}
1636
Paul Duffinb05d4292020-05-20 12:19:10 +01001637func (module *sdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin23970f42020-05-20 14:20:02 +01001638 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001639}
1640
Colin Cross79c7c262019-04-17 11:11:46 -07001641// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001642func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001643 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001644 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001645}
1646
1647// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001648func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001649 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001650 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001651}
Jiyong Parke3833882020-02-17 17:28:10 +09001652
1653//
1654// java_sdk_library_xml
1655//
1656type sdkLibraryXml struct {
1657 android.ModuleBase
1658 android.DefaultableModuleBase
1659 android.ApexModuleBase
1660
1661 properties sdkLibraryXmlProperties
1662
1663 outputFilePath android.OutputPath
1664 installDirPath android.InstallPath
1665}
1666
1667type sdkLibraryXmlProperties struct {
1668 // canonical name of the lib
1669 Lib_name *string
1670}
1671
1672// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1673// Not to be used directly by users. java_sdk_library internally uses this.
1674func sdkLibraryXmlFactory() android.Module {
1675 module := &sdkLibraryXml{}
1676
1677 module.AddProperties(&module.properties)
1678
1679 android.InitApexModule(module)
1680 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1681
1682 return module
1683}
1684
1685// from android.PrebuiltEtcModule
1686func (module *sdkLibraryXml) SubDir() string {
1687 return "permissions"
1688}
1689
1690// from android.PrebuiltEtcModule
1691func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1692 return module.outputFilePath
1693}
1694
1695// from android.ApexModule
1696func (module *sdkLibraryXml) AvailableFor(what string) bool {
1697 return true
1698}
1699
1700func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1701 // do nothing
1702}
1703
1704// File path to the runtime implementation library
1705func (module *sdkLibraryXml) implPath() string {
1706 implName := proptools.String(module.properties.Lib_name)
1707 if apexName := module.ApexName(); apexName != "" {
1708 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1709 // In most cases, this works fine. But when apex_name is set or override_apex is used
1710 // this can be wrong.
1711 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1712 }
1713 partition := "system"
1714 if module.SocSpecific() {
1715 partition = "vendor"
1716 } else if module.DeviceSpecific() {
1717 partition = "odm"
1718 } else if module.ProductSpecific() {
1719 partition = "product"
1720 } else if module.SystemExtSpecific() {
1721 partition = "system_ext"
1722 }
1723 return "/" + partition + "/framework/" + implName + ".jar"
1724}
1725
1726func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1727 libName := proptools.String(module.properties.Lib_name)
1728 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1729
1730 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1731 rule := android.NewRuleBuilder()
1732 rule.Command().
1733 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1734 Output(module.outputFilePath)
1735
1736 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1737
1738 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1739}
1740
1741func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1742 if !module.IsForPlatform() {
1743 return []android.AndroidMkEntries{android.AndroidMkEntries{
1744 Disabled: true,
1745 }}
1746 }
1747
1748 return []android.AndroidMkEntries{android.AndroidMkEntries{
1749 Class: "ETC",
1750 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1751 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1752 func(entries *android.AndroidMkEntries) {
1753 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1754 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1755 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1756 },
1757 },
1758 }}
1759}
Paul Duffindd46f712020-02-10 13:37:10 +00001760
1761type sdkLibrarySdkMemberType struct {
1762 android.SdkMemberTypeBase
1763}
1764
1765func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1766 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1767}
1768
1769func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1770 _, ok := module.(*SdkLibrary)
1771 return ok
1772}
1773
1774func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1775 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1776}
1777
1778func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1779 return &sdkLibrarySdkMemberProperties{}
1780}
1781
1782type sdkLibrarySdkMemberProperties struct {
1783 android.SdkMemberPropertiesBase
1784
1785 // Scope to per scope properties.
1786 Scopes map[*apiScope]scopeProperties
1787
1788 // Additional libraries that the exported stubs libraries depend upon.
1789 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001790
1791 // The Java stubs source files.
1792 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01001793
1794 // The naming scheme.
1795 Naming_scheme *string
Paul Duffindd46f712020-02-10 13:37:10 +00001796}
1797
1798type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001799 Jars android.Paths
1800 StubsSrcJar android.Path
1801 CurrentApiFile android.Path
1802 RemovedApiFile android.Path
1803 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001804}
1805
1806func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1807 sdk := variant.(*SdkLibrary)
1808
1809 s.Scopes = make(map[*apiScope]scopeProperties)
1810 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001811 paths := sdk.findScopePaths(apiScope)
1812 if paths == nil {
1813 continue
1814 }
1815
Paul Duffindd46f712020-02-10 13:37:10 +00001816 jars := paths.stubsImplPath
1817 if len(jars) > 0 {
1818 properties := scopeProperties{}
1819 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01001820 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01001821 properties.StubsSrcJar = paths.stubsSrcJar.Path()
1822 properties.CurrentApiFile = paths.currentApiFilePath.Path()
1823 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffindd46f712020-02-10 13:37:10 +00001824 s.Scopes[apiScope] = properties
1825 }
1826 }
1827
1828 s.Libs = sdk.properties.Libs
Paul Duffinf7a64332020-05-13 16:54:55 +01001829 s.Naming_scheme = sdk.commonProperties.Naming_scheme
Paul Duffindd46f712020-02-10 13:37:10 +00001830}
1831
1832func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01001833 if s.Naming_scheme != nil {
1834 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
1835 }
1836
Paul Duffindd46f712020-02-10 13:37:10 +00001837 for _, apiScope := range allApiScopes {
1838 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01001839 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00001840
Paul Duffin3d1248c2020-04-09 00:10:17 +01001841 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1842
Paul Duffindd46f712020-02-10 13:37:10 +00001843 var jars []string
1844 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001845 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001846 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1847 jars = append(jars, dest)
1848 }
1849 scopeSet.AddProperty("jars", jars)
1850
Paul Duffin3d1248c2020-04-09 00:10:17 +01001851 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1852 // the source files are also unpacked.
1853 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1854 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1855 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1856
Paul Duffin1fd005d2020-04-09 01:08:11 +01001857 if properties.CurrentApiFile != nil {
1858 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1859 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1860 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1861 }
1862
1863 if properties.RemovedApiFile != nil {
1864 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1865 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1866 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1867 }
1868
Paul Duffindd46f712020-02-10 13:37:10 +00001869 if properties.SdkVersion != "" {
1870 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1871 }
1872 }
1873 }
1874
1875 if len(s.Libs) > 0 {
1876 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1877 }
1878}