blob: 7e81c1e330fc50e5b6a52f0d14227aa44f23d101 [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"
Jiyong Park82484c02018-04-23 21:41:26 +090022 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090023 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025
Paul Duffind1b3a922020-01-22 11:57:20 +000026 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010028
29 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030)
31
Jooyung Han58f26ab2019-12-18 15:34:32 +090032const (
Paul Duffindd9d0742020-05-08 15:52:37 +010033 sdkXmlFileSuffix = ".xml"
34 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090035 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
36 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090037 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090038 ` you may not use this file except in compliance with the License.\n` +
39 ` You may obtain a copy of the License at\n` +
40 `\n` +
41 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
42 `\n` +
43 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090044 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090045 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
46 ` See the License for the specific language governing permissions and\n` +
47 ` limitations under the License.\n` +
48 `-->\n` +
49 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090050 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090051 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090052)
53
Paul Duffind1b3a922020-01-22 11:57:20 +000054// A tag to associated a dependency with a specific api scope.
55type scopeDependencyTag struct {
56 blueprint.BaseDependencyTag
57 name string
58 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010059
60 // Function for extracting appropriate path information from the dependency.
61 depInfoExtractor func(paths *scopePaths, dep android.Module) error
62}
63
64// Extract tag specific information from the dependency.
65func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
66 err := tag.depInfoExtractor(paths, dep)
67 if err != nil {
68 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
69 }
Paul Duffind1b3a922020-01-22 11:57:20 +000070}
71
72// Provides information about an api scope, e.g. public, system, test.
73type apiScope struct {
74 // The name of the api scope, e.g. public, system, test
75 name string
76
Paul Duffin97b53b82020-05-05 14:40:52 +010077 // The api scope that this scope extends.
78 extends *apiScope
79
Paul Duffin3375e352020-04-28 10:44:03 +010080 // The legacy enabled status for a specific scope can be dependent on other
81 // properties that have been specified on the library so it is provided by
82 // a function that can determine the status by examining those properties.
83 legacyEnabledStatus func(module *SdkLibrary) bool
84
85 // The default enabled status for non-legacy behavior, which is triggered by
86 // explicitly enabling at least one api scope.
87 defaultEnabledStatus bool
88
89 // Gets a pointer to the scope specific properties.
90 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
91
Paul Duffin46a26a82020-04-07 19:27:04 +010092 // The name of the field in the dynamically created structure.
93 fieldName string
94
Paul Duffin6b836ba2020-05-13 19:19:49 +010095 // The name of the property in the java_sdk_library_import
96 propertyName string
97
Paul Duffind1b3a922020-01-22 11:57:20 +000098 // The tag to use to depend on the stubs library module.
99 stubsTag scopeDependencyTag
100
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100101 // The tag to use to depend on the stubs source module (if separate from the API module).
102 stubsSourceTag scopeDependencyTag
103
104 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
105 apiFileTag scopeDependencyTag
106
Paul Duffinc8782502020-04-29 20:45:27 +0100107 // The tag to use to depend on the stubs source and API module.
108 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000109
110 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
111 apiFilePrefix string
112
113 // The scope specific prefix to add to the sdk library module name to construct a scope specific
114 // module name.
115 moduleSuffix string
116
Paul Duffind1b3a922020-01-22 11:57:20 +0000117 // SDK version that the stubs library is built against. Note that this is always
118 // *current. Older stubs library built with a numbered SDK version is created from
119 // the prebuilt jar.
120 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100121
122 // Extra arguments to pass to droidstubs for this scope.
123 droidstubsArgs []string
Anton Hansson6478ac12020-05-02 11:19:36 +0100124
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100125 // The args that must be passed to droidstubs to generate the stubs source
126 // for this scope.
127 //
128 // The stubs source must include the definitions of everything that is in this
129 // api scope and all the scopes that this one extends.
130 droidstubsArgsForGeneratingStubsSource []string
131
132 // The args that must be passed to droidstubs to generate the API for this scope.
133 //
134 // The API only includes the additional members that this scope adds over the scope
135 // that it extends.
136 droidstubsArgsForGeneratingApi []string
137
138 // True if the stubs source and api can be created by the same metalava invocation.
139 createStubsSourceAndApiTogether bool
140
Anton Hansson6478ac12020-05-02 11:19:36 +0100141 // Whether the api scope can be treated as unstable, and should skip compat checks.
142 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000143}
144
145// Initialize a scope, creating and adding appropriate dependency tags
146func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100147 name := scope.name
Paul Duffin6b836ba2020-05-13 19:19:49 +0100148 scope.propertyName = strings.ReplaceAll(name, "-", "_")
149 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000150 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100151 name: name + "-stubs",
152 apiScope: scope,
153 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000154 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100155 scope.stubsSourceTag = scopeDependencyTag{
156 name: name + "-stubs-source",
157 apiScope: scope,
158 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
159 }
160 scope.apiFileTag = scopeDependencyTag{
161 name: name + "-api",
162 apiScope: scope,
163 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
164 }
Paul Duffinc8782502020-04-29 20:45:27 +0100165 scope.stubsSourceAndApiTag = scopeDependencyTag{
166 name: name + "-stubs-source-and-api",
167 apiScope: scope,
168 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000169 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100170
171 // To get the args needed to generate the stubs source append all the args from
172 // this scope and all the scopes it extends as each set of args adds additional
173 // members to the stubs.
174 var stubsSourceArgs []string
175 for s := scope; s != nil; s = s.extends {
176 stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
177 }
178 scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
179
180 // Currently the args needed to generate the API are the same as the args
181 // needed to add additional members.
182 apiArgs := scope.droidstubsArgs
183 scope.droidstubsArgsForGeneratingApi = apiArgs
184
185 // If the args needed to generate the stubs and API are the same then they
186 // can be generated in a single invocation of metalava, otherwise they will
187 // need separate invocations.
188 scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
189
Paul Duffind1b3a922020-01-22 11:57:20 +0000190 return scope
191}
192
Paul Duffinc3091c82020-05-08 14:16:20 +0100193func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100194 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000195}
196
Paul Duffinc8782502020-04-29 20:45:27 +0100197func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100198 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000199}
200
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100201func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100202 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100203}
204
Paul Duffin3375e352020-04-28 10:44:03 +0100205func (scope *apiScope) String() string {
206 return scope.name
207}
208
Paul Duffind1b3a922020-01-22 11:57:20 +0000209type apiScopes []*apiScope
210
211func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
212 var list []string
213 for _, scope := range scopes {
214 list = append(list, accessor(scope))
215 }
216 return list
217}
218
Jiyong Parkc678ad32018-04-10 13:07:10 +0900219var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000220 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100221 name: "public",
222
223 // Public scope is enabled by default for both legacy and non-legacy modes.
224 legacyEnabledStatus: func(module *SdkLibrary) bool {
225 return true
226 },
227 defaultEnabledStatus: true,
228
229 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
230 return &module.sdkLibraryProperties.Public
231 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000232 sdkVersion: "current",
233 })
234 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100235 name: "system",
236 extends: apiScopePublic,
237 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
238 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
239 return &module.sdkLibraryProperties.System
240 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100241 apiFilePrefix: "system-",
Paul Duffindd9d0742020-05-08 15:52:37 +0100242 moduleSuffix: ".system",
Anton Hansson6affb1f2020-04-28 16:47:41 +0100243 sdkVersion: "system_current",
Paul Duffin0d543642020-04-29 22:18:41 +0100244 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000245 })
246 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100247 name: "test",
248 extends: apiScopePublic,
249 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
250 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
251 return &module.sdkLibraryProperties.Test
252 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100253 apiFilePrefix: "test-",
Paul Duffindd9d0742020-05-08 15:52:37 +0100254 moduleSuffix: ".test",
Anton Hansson6affb1f2020-04-28 16:47:41 +0100255 sdkVersion: "test_current",
256 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson6478ac12020-05-02 11:19:36 +0100257 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000258 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100259 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100260 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100261 extends: apiScopeSystem,
262 // Module_lib scope is disabled by default in legacy mode.
263 //
264 // Enabling this would break existing usages.
265 legacyEnabledStatus: func(module *SdkLibrary) bool {
266 return false
267 },
268 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
269 return &module.sdkLibraryProperties.Module_lib
270 },
271 apiFilePrefix: "module-lib-",
272 moduleSuffix: ".module_lib",
273 sdkVersion: "module_current",
274 droidstubsArgs: []string{
275 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
276 },
277 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000278 allApiScopes = apiScopes{
279 apiScopePublic,
280 apiScopeSystem,
281 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100282 apiScopeModuleLib,
Paul Duffind1b3a922020-01-22 11:57:20 +0000283 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900284)
285
Jiyong Park82484c02018-04-23 21:41:26 +0900286var (
287 javaSdkLibrariesLock sync.Mutex
288)
289
Jiyong Parkc678ad32018-04-10 13:07:10 +0900290// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900291// 1) disallowing linking to the runtime shared lib
292// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900293
294func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000295 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900296
Jiyong Park82484c02018-04-23 21:41:26 +0900297 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
298 javaSdkLibraries := javaSdkLibraries(ctx.Config())
299 sort.Strings(*javaSdkLibraries)
300 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
301 })
Paul Duffindd46f712020-02-10 13:37:10 +0000302
303 // Register sdk member types.
304 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
305 android.SdkMemberTypeBase{
306 PropertyName: "java_sdk_libs",
307 SupportsSdk: true,
308 },
309 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900310}
311
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000312func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
313 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
314 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
315}
316
Paul Duffin3375e352020-04-28 10:44:03 +0100317// Properties associated with each api scope.
318type ApiScopeProperties struct {
319 // Indicates whether the api surface is generated.
320 //
321 // If this is set for any scope then all scopes must explicitly specify if they
322 // are enabled. This is to prevent new usages from depending on legacy behavior.
323 //
324 // Otherwise, if this is not set for any scope then the default behavior is
325 // scope specific so please refer to the scope specific property documentation.
326 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100327
328 // The sdk_version to use for building the stubs.
329 //
330 // If not specified then it will use an sdk_version determined as follows:
331 // 1) If the sdk_version specified on the java_sdk_library is none then this
332 // will be none. This is used for java_sdk_library instances that are used
333 // to create stubs that contribute to the core_current sdk version.
334 // 2) Otherwise, it is assumed that this library extends but does not contribute
335 // directly to a specific sdk_version and so this uses the sdk_version appropriate
336 // for the api scope. e.g. public will use sdk_version: current, system will use
337 // sdk_version: system_current, etc.
338 //
339 // This does not affect the sdk_version used for either generating the stubs source
340 // or the API file. They both have to use the same sdk_version as is used for
341 // compiling the implementation library.
342 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100343}
344
Jiyong Parkc678ad32018-04-10 13:07:10 +0900345type sdkLibraryProperties struct {
Paul Duffin4911a892020-04-29 23:35:13 +0100346 // Visibility for stubs library modules. If not specified then defaults to the
347 // visibility property.
348 Stubs_library_visibility []string
349
350 // Visibility for stubs source modules. If not specified then defaults to the
351 // visibility property.
352 Stubs_source_visibility []string
353
Sundong Ahnf043cf62018-06-25 16:04:37 +0900354 // List of Java libraries that will be in the classpath when building stubs
355 Stub_only_libs []string `android:"arch_variant"`
356
Paul Duffin7a586d32019-12-30 17:09:34 +0000357 // list of package names that will be documented and publicized as API.
358 // This allows the API to be restricted to a subset of the source files provided.
359 // If this is unspecified then all the source files will be treated as being part
360 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900361 Api_packages []string
362
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900363 // list of package names that must be hidden from the API
364 Hidden_api_packages []string
365
Paul Duffin749f98f2019-12-30 17:23:46 +0000366 // the relative path to the directory containing the api specification files.
367 // Defaults to "api".
368 Api_dir *string
369
Paul Duffin43db9be2019-12-30 17:35:49 +0000370 // If set to true there is no runtime library.
371 Api_only *bool
372
Paul Duffin11512472019-02-11 15:55:17 +0000373 // local files that are used within user customized droiddoc options.
374 Droiddoc_option_files []string
375
376 // additional droiddoc options
377 // Available variables for substitution:
378 //
379 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900380 Droiddoc_options []string
381
Sundong Ahn054b19a2018-10-19 13:46:09 +0900382 // a list of top-level directories containing files to merge qualifier annotations
383 // (i.e. those intended to be included in the stubs written) from.
384 Merge_annotations_dirs []string
385
386 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
387 Merge_inclusion_annotations_dirs []string
388
389 // If set to true, the path of dist files is apistubs/core. Defaults to false.
390 Core_lib *bool
391
Sundong Ahn80a87b32019-05-13 15:02:50 +0900392 // don't create dist rules.
393 No_dist *bool `blueprint:"mutated"`
394
Paul Duffin3375e352020-04-28 10:44:03 +0100395 // indicates whether system and test apis should be generated.
396 Generate_system_and_test_apis bool `blueprint:"mutated"`
397
398 // The properties specific to the public api scope
399 //
400 // Unless explicitly specified by using public.enabled the public api scope is
401 // enabled by default in both legacy and non-legacy mode.
402 Public ApiScopeProperties
403
404 // The properties specific to the system api scope
405 //
406 // In legacy mode the system api scope is enabled by default when sdk_version
407 // is set to something other than "none".
408 //
409 // In non-legacy mode the system api scope is disabled by default.
410 System ApiScopeProperties
411
412 // The properties specific to the test api scope
413 //
414 // In legacy mode the test api scope is enabled by default when sdk_version
415 // is set to something other than "none".
416 //
417 // In non-legacy mode the test api scope is disabled by default.
418 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000419
Paul Duffin8f265b92020-04-28 14:13:56 +0100420 // The properties specific to the module_lib api scope
421 //
422 // Unless explicitly specified by using test.enabled the module_lib api scope is
423 // disabled by default.
424 Module_lib ApiScopeProperties
425
Paul Duffin160fe412020-05-10 19:32:20 +0100426 // Properties related to api linting.
427 Api_lint struct {
428 // Enable api linting.
429 Enabled *bool
430 }
431
Jiyong Parkc678ad32018-04-10 13:07:10 +0900432 // TODO: determines whether to create HTML doc or not
433 //Html_doc *bool
434}
435
Paul Duffind1b3a922020-01-22 11:57:20 +0000436type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100437 stubsHeaderPath android.Paths
438 stubsImplPath android.Paths
439 currentApiFilePath android.Path
440 removedApiFilePath android.Path
441 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000442}
443
Paul Duffinc8782502020-04-29 20:45:27 +0100444func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
445 if lib, ok := dep.(Dependency); ok {
446 paths.stubsHeaderPath = lib.HeaderJars()
447 paths.stubsImplPath = lib.ImplementationJars()
448 return nil
449 } else {
450 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
451 }
452}
453
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100454func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
455 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
456 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100457 return nil
458 } else {
459 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
460 }
461}
462
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100463func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
464 paths.currentApiFilePath = provider.ApiFilePath()
465 paths.removedApiFilePath = provider.RemovedApiFilePath()
466}
467
468func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
469 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
470 paths.extractApiInfoFromApiStubsProvider(provider)
471 })
472}
473
474func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsProvider) {
475 paths.stubsSrcJar = provider.StubsSrcJar()
476}
477
478func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
479 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
480 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
481 })
482}
483
484func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
485 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
486 paths.extractApiInfoFromApiStubsProvider(provider)
487 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
488 })
489}
490
491type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100492 // The naming scheme to use for the components that this module creates.
493 //
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100494 // If not specified then it defaults to "default". The other allowable value is
495 // "framework-modules" which matches the scheme currently used by framework modules
496 // for the equivalent components represented as separate Soong modules.
Paul Duffin1b1e8062020-05-08 13:44:43 +0100497 //
498 // This is a temporary mechanism to simplify conversion from separate modules for each
499 // component that follow a different naming pattern to the default one.
500 //
501 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100502 Naming_scheme *string
503}
504
Paul Duffin56d44902020-01-31 13:36:25 +0000505// Common code between sdk library and sdk library import
506type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100507 moduleBase *android.ModuleBase
508
Paul Duffin56d44902020-01-31 13:36:25 +0000509 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100510
511 namingScheme sdkLibraryComponentNamingScheme
512
513 commonProperties commonToSdkLibraryAndImportProperties
Paul Duffin56d44902020-01-31 13:36:25 +0000514}
515
Paul Duffinc3091c82020-05-08 14:16:20 +0100516func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
517 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100518
519 moduleBase.AddProperties(&c.commonProperties)
520}
521
522func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
523 schemeProperty := proptools.StringDefault(c.commonProperties.Naming_scheme, "default")
524 switch schemeProperty {
525 case "default":
526 c.namingScheme = &defaultNamingScheme{}
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100527 case "framework-modules":
528 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1b1e8062020-05-08 13:44:43 +0100529 default:
530 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
531 return false
532 }
533
534 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100535}
536
537// Name of the java_library module that compiles the stubs source.
538func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100539 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100540}
541
542// Name of the droidstubs module that generates the stubs source and may also
543// generate/check the API.
544func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100545 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100546}
547
548// Name of the droidstubs module that generates/checks the API. Only used if it
549// requires different arts to the stubs source generating module.
550func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100551 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100552}
553
Paul Duffin56d44902020-01-31 13:36:25 +0000554func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
555 if c.scopePaths == nil {
556 c.scopePaths = make(map[*apiScope]*scopePaths)
557 }
558 paths := c.scopePaths[scope]
559 if paths == nil {
560 paths = &scopePaths{}
561 c.scopePaths[scope] = paths
562 }
563
564 return paths
565}
566
Inseob Kimc0907f12019-02-08 21:00:45 +0900567type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900568 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900569
Sundong Ahn054b19a2018-10-19 13:46:09 +0900570 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900571
Paul Duffin3375e352020-04-28 10:44:03 +0100572 // Map from api scope to the scope specific property structure.
573 scopeToProperties map[*apiScope]*ApiScopeProperties
574
Paul Duffin56d44902020-01-31 13:36:25 +0000575 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900576}
577
Inseob Kimc0907f12019-02-08 21:00:45 +0900578var _ Dependency = (*SdkLibrary)(nil)
579var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800580
Paul Duffin3375e352020-04-28 10:44:03 +0100581func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
582 return module.sdkLibraryProperties.Generate_system_and_test_apis
583}
584
585func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
586 // Check to see if any scopes have been explicitly enabled. If any have then all
587 // must be.
588 anyScopesExplicitlyEnabled := false
589 for _, scope := range allApiScopes {
590 scopeProperties := module.scopeToProperties[scope]
591 if scopeProperties.Enabled != nil {
592 anyScopesExplicitlyEnabled = true
593 break
594 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000595 }
Paul Duffin3375e352020-04-28 10:44:03 +0100596
597 var generatedScopes apiScopes
598 enabledScopes := make(map[*apiScope]struct{})
599 for _, scope := range allApiScopes {
600 scopeProperties := module.scopeToProperties[scope]
601 // If any scopes are explicitly enabled then ignore the legacy enabled status.
602 // This is to ensure that any new usages of this module type do not rely on legacy
603 // behaviour.
604 defaultEnabledStatus := false
605 if anyScopesExplicitlyEnabled {
606 defaultEnabledStatus = scope.defaultEnabledStatus
607 } else {
608 defaultEnabledStatus = scope.legacyEnabledStatus(module)
609 }
610 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
611 if enabled {
612 enabledScopes[scope] = struct{}{}
613 generatedScopes = append(generatedScopes, scope)
614 }
615 }
616
617 // Now check to make sure that any scope that is extended by an enabled scope is also
618 // enabled.
619 for _, scope := range allApiScopes {
620 if _, ok := enabledScopes[scope]; ok {
621 extends := scope.extends
622 if extends != nil {
623 if _, ok := enabledScopes[extends]; !ok {
624 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
625 }
626 }
627 }
628 }
629
630 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000631}
632
Paul Duffine74ac732020-02-06 13:51:46 +0000633var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
634
Jiyong Parke3833882020-02-17 17:28:10 +0900635func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
636 if dt, ok := depTag.(dependencyTag); ok {
637 return dt == xmlPermissionsFileTag
638 }
639 return false
640}
641
Inseob Kimc0907f12019-02-08 21:00:45 +0900642func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100643 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000644 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100645 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000646
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100647 // If the stubs source and API cannot be generated together then add an additional dependency on
648 // the API module.
649 if apiScope.createStubsSourceAndApiTogether {
650 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinc3091c82020-05-08 14:16:20 +0100651 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100652 } else {
653 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinc3091c82020-05-08 14:16:20 +0100654 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
655 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100656 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900657 }
658
Paul Duffine74ac732020-02-06 13:51:46 +0000659 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
660 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900661 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000662 }
663
Sundong Ahn054b19a2018-10-19 13:46:09 +0900664 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900665}
666
Inseob Kimc0907f12019-02-08 21:00:45 +0900667func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000668 // Don't build an implementation library if this is api only.
669 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
670 module.Library.GenerateAndroidBuildActions(ctx)
671 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900672
Sundong Ahn57368eb2018-07-06 11:20:23 +0900673 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000674 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900675 // the recorded paths will be returned depending on the link type of the caller.
676 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900677 tag := ctx.OtherModuleDependencyTag(to)
678
Paul Duffinc8782502020-04-29 20:45:27 +0100679 // Extract information from any of the scope specific dependencies.
680 if scopeTag, ok := tag.(scopeDependencyTag); ok {
681 apiScope := scopeTag.apiScope
682 scopePaths := module.getScopePaths(apiScope)
683
684 // Extract information from the dependency. The exact information extracted
685 // is determined by the nature of the dependency which is determined by the tag.
686 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900687 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900688 })
689}
690
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900691func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000692 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
693 return nil
694 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900695 entriesList := module.Library.AndroidMkEntries()
696 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700697 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900698 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900699}
700
Jiyong Parkc678ad32018-04-10 13:07:10 +0900701// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900702func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900703 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900704}
705
Jiyong Parkc678ad32018-04-10 13:07:10 +0900706// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900707func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900708 return module.BaseModuleName() + sdkXmlFileSuffix
709}
710
Anton Hansson5fd5d242020-03-27 19:43:19 +0000711// The dist path of the stub artifacts
712func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
713 if module.ModuleBase.Owner() != "" {
714 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
715 } else if Bool(module.sdkLibraryProperties.Core_lib) {
716 return path.Join("apistubs", "core", apiScope.name)
717 } else {
718 return path.Join("apistubs", "android", apiScope.name)
719 }
720}
721
Paul Duffin12ceb462019-12-24 20:31:31 +0000722// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +0100723func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +0100724 scopeProperties := module.scopeToProperties[apiScope]
725 if scopeProperties.Sdk_version != nil {
726 return proptools.String(scopeProperties.Sdk_version)
727 }
728
Paul Duffin12ceb462019-12-24 20:31:31 +0000729 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
730 if sdkDep.hasStandardLibs() {
731 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000732 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000733 } else {
734 // Otherwise, use no system module.
735 return "none"
736 }
737}
738
Paul Duffind1b3a922020-01-22 11:57:20 +0000739func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
740 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900741}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900742
Paul Duffind1b3a922020-01-22 11:57:20 +0000743func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
744 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900745}
746
747// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100748func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900749 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900750 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100751 Visibility []string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900752 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000753 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900754 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000755 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000756 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900757 Libs []string
758 Soc_specific *bool
759 Device_specific *bool
760 Product_specific *bool
761 System_ext_specific *bool
762 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900763 Java_version *string
764 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900765 Pdk struct {
766 Enabled *bool
767 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900768 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900769 Openjdk9 struct {
770 Srcs []string
771 Javacflags []string
772 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000773 Dist struct {
774 Targets []string
775 Dest *string
776 Dir *string
777 Tag *string
778 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900779 }{}
780
Paul Duffinc3091c82020-05-08 14:16:20 +0100781 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100782
783 // If stubs_library_visibility is not set then the created module will use the
784 // visibility of this module.
785 visibility := module.sdkLibraryProperties.Stubs_library_visibility
786 props.Visibility = visibility
787
Jiyong Parkc678ad32018-04-10 13:07:10 +0900788 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +0100789 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000790 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100791 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000792 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000793 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000794 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900795 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900796 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900797 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
798 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
799 props.Java_version = module.Library.Module.properties.Java_version
800 if module.Library.Module.deviceProperties.Compile_dex != nil {
801 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900802 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900803
804 if module.SocSpecific() {
805 props.Soc_specific = proptools.BoolPtr(true)
806 } else if module.DeviceSpecific() {
807 props.Device_specific = proptools.BoolPtr(true)
808 } else if module.ProductSpecific() {
809 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900810 } else if module.SystemExtSpecific() {
811 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900812 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000813 // Dist the class jar artifact for sdk builds.
814 if !Bool(module.sdkLibraryProperties.No_dist) {
815 props.Dist.Targets = []string{"sdk", "win_sdk"}
816 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
817 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
818 props.Dist.Tag = proptools.StringPtr(".jar")
819 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900820
Colin Cross84dfc3d2019-09-25 11:33:01 -0700821 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900822}
823
Paul Duffin6d0886e2020-04-07 18:49:53 +0100824// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +0100825// files and also updates and checks the API specification files.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100826func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900827 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900828 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100829 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900830 Srcs []string
831 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100832 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000833 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900834 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000835 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900836 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900837 Java_version *string
838 Merge_annotations_dirs []string
839 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100840 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +0900841 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900842 Current ApiToCheck
843 Last_released ApiToCheck
844 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100845
846 Api_lint struct {
847 Enabled *bool
848 New_since *string
849 Baseline_file *string
850 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900851 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900852 Aidl struct {
853 Include_dirs []string
854 Local_include_dirs []string
855 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000856 Dist struct {
857 Targets []string
858 Dest *string
859 Dir *string
860 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900861 }{}
862
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100863 // The stubs source processing uses the same compile time classpath when extracting the
864 // API from the implementation library as it does when compiling it. i.e. the same
865 // * sdk version
866 // * system_modules
867 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100868
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100869 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +0100870
871 // If stubs_source_visibility is not set then the created module will use the
872 // visibility of this module.
873 visibility := module.sdkLibraryProperties.Stubs_source_visibility
874 props.Visibility = visibility
875
Sundong Ahn054b19a2018-10-19 13:46:09 +0900876 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100877 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000878 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900879 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900880 // A droiddoc module has only one Libs property and doesn't distinguish between
881 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900882 props.Libs = module.Library.Module.properties.Libs
883 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
884 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
885 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900886 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900887
Sundong Ahn054b19a2018-10-19 13:46:09 +0900888 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
889 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
890
Paul Duffin6d0886e2020-04-07 18:49:53 +0100891 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000892 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100893 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000894 }
895 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100896 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000897 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
898 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100899 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000900 disabledWarnings := []string{
901 "MissingPermission",
902 "BroadcastBehavior",
903 "HiddenSuperclass",
904 "DeprecationMismatch",
905 "UnavailableSymbol",
906 "SdkConstant",
907 "HiddenTypeParameter",
908 "Todo",
909 "Typo",
910 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100911 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900912
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100913 if !createStubSources {
914 // Stubs are not required.
915 props.Generate_stubs = proptools.BoolPtr(false)
916 }
917
Paul Duffin1fb487d2020-04-07 18:50:10 +0100918 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100919 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000920 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100921 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900922
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100923 if createApi {
924 // List of APIs identified from the provided source files are created. They are later
925 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
926 // last-released (a.k.a numbered) list of API.
927 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
928 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
929 apiDir := module.getApiDir()
930 currentApiFileName = path.Join(apiDir, currentApiFileName)
931 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900932
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100933 // check against the not-yet-release API
934 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
935 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900936
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100937 if !apiScope.unstable {
938 // check against the latest released API
939 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
940 props.Check_api.Last_released.Api_file = latestApiFilegroupName
941 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
942 module.latestRemovedApiFilegroupName(apiScope))
943 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +0100944
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100945 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
946 // Enable api lint.
947 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
948 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +0100949
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100950 // If it exists then pass a lint-baseline.txt through to droidstubs.
951 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
952 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
953 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
954 if err != nil {
955 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
956 }
957 if len(paths) == 1 {
958 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
959 } else if len(paths) != 0 {
960 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
961 }
Paul Duffin160fe412020-05-10 19:32:20 +0100962 }
963 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900964
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100965 // Dist the api txt artifact for sdk builds.
966 if !Bool(module.sdkLibraryProperties.No_dist) {
967 props.Dist.Targets = []string{"sdk", "win_sdk"}
968 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
969 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
970 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000971 }
972
Colin Cross84dfc3d2019-09-25 11:33:01 -0700973 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900974}
975
Jooyung Han5e9013b2020-03-10 06:23:13 +0900976func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
977 depTag := mctx.OtherModuleDependencyTag(dep)
978 if depTag == xmlPermissionsFileTag {
979 return true
980 }
981 return module.Library.DepIsInSameApex(mctx, dep)
982}
983
Jiyong Parkc678ad32018-04-10 13:07:10 +0900984// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +0100985func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900986 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900987 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900988 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900989 Soc_specific *bool
990 Device_specific *bool
991 Product_specific *bool
992 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900993 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900994 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900995 Name: proptools.StringPtr(module.xmlFileName()),
996 Lib_name: proptools.StringPtr(module.BaseModuleName()),
997 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900998 }
Jiyong Parke3833882020-02-17 17:28:10 +0900999
1000 if module.SocSpecific() {
1001 props.Soc_specific = proptools.BoolPtr(true)
1002 } else if module.DeviceSpecific() {
1003 props.Device_specific = proptools.BoolPtr(true)
1004 } else if module.ProductSpecific() {
1005 props.Product_specific = proptools.BoolPtr(true)
1006 } else if module.SystemExtSpecific() {
1007 props.System_ext_specific = proptools.BoolPtr(true)
1008 }
1009
1010 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001011}
1012
Paul Duffin50061512020-01-21 16:31:05 +00001013func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001014 var ver sdkVersion
1015 var kind sdkKind
1016 if s.usePrebuilt(ctx) {
1017 ver = s.version
1018 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001019 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001020 // We don't have prebuilt SDK for the specific sdkVersion.
1021 // Instead of breaking the build, fallback to use "system_current"
1022 ver = sdkVersionCurrent
1023 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001024 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001025
1026 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001027 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001028 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001029 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001030 if ctx.Config().AllowMissingDependencies() {
1031 return android.Paths{android.PathForSource(ctx, jar)}
1032 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001033 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001034 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001035 return nil
1036 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001037 return android.Paths{jarPath.Path()}
1038}
1039
Paul Duffind1b3a922020-01-22 11:57:20 +00001040func (module *SdkLibrary) sdkJars(
1041 ctx android.BaseModuleContext,
1042 sdkVersion sdkSpec,
1043 headerJars bool) android.Paths {
1044
Paul Duffin50061512020-01-21 16:31:05 +00001045 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1046 if sdkVersion.version.isNumbered() {
1047 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001048 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +00001049 if !sdkVersion.specified() {
1050 if headerJars {
1051 return module.Library.HeaderJars()
1052 } else {
1053 return module.Library.ImplementationJars()
1054 }
1055 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001056 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +09001057 switch sdkVersion.kind {
1058 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +00001059 apiScope = apiScopeSystem
1060 case sdkTest:
1061 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +09001062 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +09001063 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +09001064 default:
Paul Duffin726d23c2020-01-22 16:30:37 +00001065 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +00001066 }
1067
Paul Duffin726d23c2020-01-22 16:30:37 +00001068 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +00001069 if headerJars {
1070 return paths.stubsHeaderPath
1071 } else {
1072 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +09001073 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001074 }
1075}
1076
Sundong Ahn241cd372018-07-13 16:16:44 +09001077// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001078func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1079 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1080}
1081
1082// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001083func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001084 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001085}
1086
Sundong Ahn80a87b32019-05-13 15:02:50 +09001087func (module *SdkLibrary) SetNoDist() {
1088 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1089}
1090
Colin Cross571cccf2019-02-04 11:22:08 -08001091var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1092
Jiyong Park82484c02018-04-23 21:41:26 +09001093func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001094 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001095 return &[]string{}
1096 }).(*[]string)
1097}
1098
Paul Duffin749f98f2019-12-30 17:23:46 +00001099func (module *SdkLibrary) getApiDir() string {
1100 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1101}
1102
Jiyong Parkc678ad32018-04-10 13:07:10 +09001103// For a java_sdk_library module, create internal modules for stubs, docs,
1104// runtime libs and xml file. If requested, the stubs and docs are created twice
1105// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001106func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1107 // If the module has been disabled then don't create any child modules.
1108 if !module.Enabled() {
1109 return
1110 }
1111
Inseob Kim6e93ac92019-03-21 17:43:49 +09001112 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001113 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001114 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001115 }
1116
Paul Duffin37e0b772019-12-30 17:20:10 +00001117 // If this builds against standard libraries (i.e. is not part of the core libraries)
1118 // then assume it provides both system and test apis. Otherwise, assume it does not and
1119 // also assume it does not contribute to the dist build.
1120 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1121 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001122 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001123 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1124
Inseob Kim8098faa2019-03-18 10:19:51 +09001125 missing_current_api := false
1126
Paul Duffin3375e352020-04-28 10:44:03 +01001127 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001128
Paul Duffin749f98f2019-12-30 17:23:46 +00001129 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001130 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001131 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001132 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001133 p := android.ExistentPathForSource(mctx, path)
1134 if !p.Valid() {
1135 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1136 missing_current_api = true
1137 }
1138 }
1139 }
1140
1141 if missing_current_api {
1142 script := "build/soong/scripts/gen-java-current-api-files.sh"
1143 p := android.ExistentPathForSource(mctx, script)
1144
1145 if !p.Valid() {
1146 panic(fmt.Sprintf("script file %s doesn't exist", script))
1147 }
1148
1149 mctx.ModuleErrorf("One or more current api files are missing. "+
1150 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001151 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001152 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001153 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001154 return
1155 }
1156
Paul Duffin3375e352020-04-28 10:44:03 +01001157 for _, scope := range generatedScopes {
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001158 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinc3091c82020-05-08 14:16:20 +01001159 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001160
1161 // If the args needed to generate the stubs and API are the same then they
1162 // can be generated in a single invocation of metalava, otherwise they will
1163 // need separate invocations.
1164 if scope.createStubsSourceAndApiTogether {
1165 // Use the stubs source name for legacy reasons.
1166 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1167 } else {
1168 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1169
1170 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinc3091c82020-05-08 14:16:20 +01001171 apiName := module.apiModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001172 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1173 }
1174
Paul Duffind1b3a922020-01-22 11:57:20 +00001175 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001176 }
1177
Paul Duffin43db9be2019-12-30 17:35:49 +00001178 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
1179 // for runtime
1180 module.createXmlFile(mctx)
1181
1182 // record java_sdk_library modules so that they are exported to make
1183 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1184 javaSdkLibrariesLock.Lock()
1185 defer javaSdkLibrariesLock.Unlock()
1186 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1187 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001188}
1189
1190func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001191 module.AddProperties(
1192 &module.sdkLibraryProperties,
1193 &module.Library.Module.properties,
1194 &module.Library.Module.dexpreoptProperties,
1195 &module.Library.Module.deviceProperties,
1196 &module.Library.Module.protoProperties,
1197 )
1198
1199 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
1200 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001201}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001202
Paul Duffin1b1e8062020-05-08 13:44:43 +01001203// Defines how to name the individual component modules the sdk library creates.
1204type sdkLibraryComponentNamingScheme interface {
1205 stubsLibraryModuleName(scope *apiScope, baseName string) string
1206
1207 stubsSourceModuleName(scope *apiScope, baseName string) string
1208
1209 apiModuleName(scope *apiScope, baseName string) string
1210}
1211
1212type defaultNamingScheme struct {
1213}
1214
1215func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1216 return scope.stubsLibraryModuleName(baseName)
1217}
1218
1219func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1220 return scope.stubsSourceModuleName(baseName)
1221}
1222
1223func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1224 return scope.apiModuleName(baseName)
1225}
1226
1227var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1228
Paul Duffin6c9c5fc2020-05-08 15:36:30 +01001229type frameworkModulesNamingScheme struct {
1230}
1231
1232func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1233 suffix := scope.name
1234 if scope == apiScopeModuleLib {
1235 suffix = "module_libs_"
1236 }
1237 return suffix
1238}
1239
1240func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1241 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1242}
1243
1244func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1245 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1246}
1247
1248func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1249 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1250}
1251
1252var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1253
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001254// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1255// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1256// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1257// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1258// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001259func SdkLibraryFactory() android.Module {
1260 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001261
1262 // Initialize information common between source and prebuilt.
1263 module.initCommon(&module.ModuleBase)
1264
Inseob Kimc0907f12019-02-08 21:00:45 +09001265 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001266 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001267 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001268
1269 // Initialize the map from scope to scope specific properties.
1270 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1271 for _, scope := range allApiScopes {
1272 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1273 }
1274 module.scopeToProperties = scopeToProperties
1275
Paul Duffin4911a892020-04-29 23:35:13 +01001276 // Add the properties containing visibility rules so that they are checked.
1277 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1278 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1279
Paul Duffin1b1e8062020-05-08 13:44:43 +01001280 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
1281 if module.initCommonAfterDefaultsApplied(ctx) {
1282 module.CreateInternalModules(ctx)
1283 }
1284 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001285 return module
1286}
Colin Cross79c7c262019-04-17 11:11:46 -07001287
1288//
1289// SDK library prebuilts
1290//
1291
Paul Duffin56d44902020-01-31 13:36:25 +00001292// Properties associated with each api scope.
1293type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001294 Jars []string `android:"path"`
1295
1296 Sdk_version *string
1297
Colin Cross79c7c262019-04-17 11:11:46 -07001298 // List of shared java libs that this module has dependencies to
1299 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001300
Paul Duffinc8782502020-04-29 20:45:27 +01001301 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001302 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001303
1304 // The current.txt
1305 Current_api string `android:"path"`
1306
1307 // The removed.txt
1308 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001309}
1310
Paul Duffin56d44902020-01-31 13:36:25 +00001311type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001312 // List of shared java libs, common to all scopes, that this module has
1313 // dependencies to
1314 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001315}
1316
Colin Cross79c7c262019-04-17 11:11:46 -07001317type sdkLibraryImport struct {
1318 android.ModuleBase
1319 android.DefaultableModuleBase
1320 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001321 android.ApexModuleBase
1322 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001323
1324 properties sdkLibraryImportProperties
1325
Paul Duffin46a26a82020-04-07 19:27:04 +01001326 // Map from api scope to the scope specific property structure.
1327 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1328
Paul Duffin56d44902020-01-31 13:36:25 +00001329 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001330}
1331
1332var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1333
Paul Duffin46a26a82020-04-07 19:27:04 +01001334// The type of a structure that contains a field of type sdkLibraryScopeProperties
1335// for each apiscope in allApiScopes, e.g. something like:
1336// struct {
1337// Public sdkLibraryScopeProperties
1338// System sdkLibraryScopeProperties
1339// ...
1340// }
1341var allScopeStructType = createAllScopePropertiesStructType()
1342
1343// Dynamically create a structure type for each apiscope in allApiScopes.
1344func createAllScopePropertiesStructType() reflect.Type {
1345 var fields []reflect.StructField
1346 for _, apiScope := range allApiScopes {
1347 field := reflect.StructField{
1348 Name: apiScope.fieldName,
1349 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1350 }
1351 fields = append(fields, field)
1352 }
1353
1354 return reflect.StructOf(fields)
1355}
1356
1357// Create an instance of the scope specific structure type and return a map
1358// from apiscope to a pointer to each scope specific field.
1359func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1360 allScopePropertiesPtr := reflect.New(allScopeStructType)
1361 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1362 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1363
1364 for _, apiScope := range allApiScopes {
1365 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1366 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1367 }
1368
1369 return allScopePropertiesPtr.Interface(), scopeProperties
1370}
1371
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001372// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001373func sdkLibraryImportFactory() android.Module {
1374 module := &sdkLibraryImport{}
1375
Paul Duffin46a26a82020-04-07 19:27:04 +01001376 allScopeProperties, scopeToProperties := createPropertiesInstance()
1377 module.scopeProperties = scopeToProperties
1378 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001379
Paul Duffinc3091c82020-05-08 14:16:20 +01001380 // Initialize information common between source and prebuilt.
1381 module.initCommon(&module.ModuleBase)
1382
Paul Duffin0bdcb272020-02-06 15:24:57 +00001383 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001384 android.InitApexModule(module)
1385 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001386 InitJavaModule(module, android.HostAndDeviceSupported)
1387
Paul Duffin1b1e8062020-05-08 13:44:43 +01001388 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1389 if module.initCommonAfterDefaultsApplied(mctx) {
1390 module.createInternalModules(mctx)
1391 }
1392 })
Colin Cross79c7c262019-04-17 11:11:46 -07001393 return module
1394}
1395
1396func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1397 return &module.prebuilt
1398}
1399
1400func (module *sdkLibraryImport) Name() string {
1401 return module.prebuilt.Name(module.ModuleBase.Name())
1402}
1403
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001404func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001405
Paul Duffin50061512020-01-21 16:31:05 +00001406 // If the build is configured to use prebuilts then force this to be preferred.
1407 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1408 module.prebuilt.ForcePrefer()
1409 }
1410
Paul Duffin46a26a82020-04-07 19:27:04 +01001411 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001412 if len(scopeProperties.Jars) == 0 {
1413 continue
1414 }
1415
Paul Duffinbbb546b2020-04-09 00:07:11 +01001416 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001417
1418 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +00001419 }
Colin Cross79c7c262019-04-17 11:11:46 -07001420
1421 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1422 javaSdkLibrariesLock.Lock()
1423 defer javaSdkLibrariesLock.Unlock()
1424 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1425}
1426
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001427func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001428 // Creates a java import for the jar with ".stubs" suffix
1429 props := struct {
1430 Name *string
1431 Soc_specific *bool
1432 Device_specific *bool
1433 Product_specific *bool
1434 System_ext_specific *bool
1435 Sdk_version *string
1436 Libs []string
1437 Jars []string
1438 Prefer *bool
1439 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001440 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001441 props.Sdk_version = scopeProperties.Sdk_version
1442 // Prepend any of the libs from the legacy public properties to the libs for each of the
1443 // scopes to avoid having to duplicate them in each scope.
1444 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1445 props.Jars = scopeProperties.Jars
1446 if module.SocSpecific() {
1447 props.Soc_specific = proptools.BoolPtr(true)
1448 } else if module.DeviceSpecific() {
1449 props.Device_specific = proptools.BoolPtr(true)
1450 } else if module.ProductSpecific() {
1451 props.Product_specific = proptools.BoolPtr(true)
1452 } else if module.SystemExtSpecific() {
1453 props.System_ext_specific = proptools.BoolPtr(true)
1454 }
Paul Duffin38b57852020-05-13 16:08:09 +01001455 // The imports are preferred if the java_sdk_library_import is preferred.
1456 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001457 mctx.CreateModule(ImportFactory, &props)
1458}
1459
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001460func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001461 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001462 Name *string
1463 Srcs []string
1464 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001465 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001466 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001467 props.Srcs = scopeProperties.Stub_srcs
1468 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001469
1470 // The stubs source is preferred if the java_sdk_library_import is preferred.
1471 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001472}
1473
Colin Cross79c7c262019-04-17 11:11:46 -07001474func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001475 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001476 if len(scopeProperties.Jars) == 0 {
1477 continue
1478 }
1479
1480 // Add dependencies to the prebuilt stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001481 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin56d44902020-01-31 13:36:25 +00001482 }
Colin Cross79c7c262019-04-17 11:11:46 -07001483}
1484
1485func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1486 // Record the paths to the prebuilt stubs library.
1487 ctx.VisitDirectDeps(func(to android.Module) {
1488 tag := ctx.OtherModuleDependencyTag(to)
1489
Paul Duffin56d44902020-01-31 13:36:25 +00001490 if lib, ok := to.(Dependency); ok {
1491 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1492 apiScope := scopeTag.apiScope
1493 scopePaths := module.getScopePaths(apiScope)
1494 scopePaths.stubsHeaderPath = lib.HeaderJars()
1495 }
Colin Cross79c7c262019-04-17 11:11:46 -07001496 }
1497 })
1498}
1499
Paul Duffin56d44902020-01-31 13:36:25 +00001500func (module *sdkLibraryImport) sdkJars(
1501 ctx android.BaseModuleContext,
1502 sdkVersion sdkSpec) android.Paths {
1503
Paul Duffin50061512020-01-21 16:31:05 +00001504 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1505 if sdkVersion.version.isNumbered() {
1506 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1507 }
1508
Paul Duffin56d44902020-01-31 13:36:25 +00001509 var apiScope *apiScope
1510 switch sdkVersion.kind {
1511 case sdkSystem:
1512 apiScope = apiScopeSystem
1513 case sdkTest:
1514 apiScope = apiScopeTest
1515 default:
1516 apiScope = apiScopePublic
1517 }
1518
1519 paths := module.getScopePaths(apiScope)
1520 return paths.stubsHeaderPath
1521}
1522
Colin Cross79c7c262019-04-17 11:11:46 -07001523// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001524func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001525 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001526 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001527}
1528
1529// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001530func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001531 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001532 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001533}
Jiyong Parke3833882020-02-17 17:28:10 +09001534
1535//
1536// java_sdk_library_xml
1537//
1538type sdkLibraryXml struct {
1539 android.ModuleBase
1540 android.DefaultableModuleBase
1541 android.ApexModuleBase
1542
1543 properties sdkLibraryXmlProperties
1544
1545 outputFilePath android.OutputPath
1546 installDirPath android.InstallPath
1547}
1548
1549type sdkLibraryXmlProperties struct {
1550 // canonical name of the lib
1551 Lib_name *string
1552}
1553
1554// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1555// Not to be used directly by users. java_sdk_library internally uses this.
1556func sdkLibraryXmlFactory() android.Module {
1557 module := &sdkLibraryXml{}
1558
1559 module.AddProperties(&module.properties)
1560
1561 android.InitApexModule(module)
1562 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1563
1564 return module
1565}
1566
1567// from android.PrebuiltEtcModule
1568func (module *sdkLibraryXml) SubDir() string {
1569 return "permissions"
1570}
1571
1572// from android.PrebuiltEtcModule
1573func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1574 return module.outputFilePath
1575}
1576
1577// from android.ApexModule
1578func (module *sdkLibraryXml) AvailableFor(what string) bool {
1579 return true
1580}
1581
1582func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1583 // do nothing
1584}
1585
1586// File path to the runtime implementation library
1587func (module *sdkLibraryXml) implPath() string {
1588 implName := proptools.String(module.properties.Lib_name)
1589 if apexName := module.ApexName(); apexName != "" {
1590 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1591 // In most cases, this works fine. But when apex_name is set or override_apex is used
1592 // this can be wrong.
1593 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1594 }
1595 partition := "system"
1596 if module.SocSpecific() {
1597 partition = "vendor"
1598 } else if module.DeviceSpecific() {
1599 partition = "odm"
1600 } else if module.ProductSpecific() {
1601 partition = "product"
1602 } else if module.SystemExtSpecific() {
1603 partition = "system_ext"
1604 }
1605 return "/" + partition + "/framework/" + implName + ".jar"
1606}
1607
1608func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1609 libName := proptools.String(module.properties.Lib_name)
1610 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1611
1612 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1613 rule := android.NewRuleBuilder()
1614 rule.Command().
1615 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1616 Output(module.outputFilePath)
1617
1618 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1619
1620 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1621}
1622
1623func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1624 if !module.IsForPlatform() {
1625 return []android.AndroidMkEntries{android.AndroidMkEntries{
1626 Disabled: true,
1627 }}
1628 }
1629
1630 return []android.AndroidMkEntries{android.AndroidMkEntries{
1631 Class: "ETC",
1632 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1633 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1634 func(entries *android.AndroidMkEntries) {
1635 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1636 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1637 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1638 },
1639 },
1640 }}
1641}
Paul Duffindd46f712020-02-10 13:37:10 +00001642
1643type sdkLibrarySdkMemberType struct {
1644 android.SdkMemberTypeBase
1645}
1646
1647func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1648 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1649}
1650
1651func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1652 _, ok := module.(*SdkLibrary)
1653 return ok
1654}
1655
1656func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1657 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1658}
1659
1660func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1661 return &sdkLibrarySdkMemberProperties{}
1662}
1663
1664type sdkLibrarySdkMemberProperties struct {
1665 android.SdkMemberPropertiesBase
1666
1667 // Scope to per scope properties.
1668 Scopes map[*apiScope]scopeProperties
1669
1670 // Additional libraries that the exported stubs libraries depend upon.
1671 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001672
1673 // The Java stubs source files.
1674 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01001675
1676 // The naming scheme.
1677 Naming_scheme *string
Paul Duffindd46f712020-02-10 13:37:10 +00001678}
1679
1680type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001681 Jars android.Paths
1682 StubsSrcJar android.Path
1683 CurrentApiFile android.Path
1684 RemovedApiFile android.Path
1685 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001686}
1687
1688func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1689 sdk := variant.(*SdkLibrary)
1690
1691 s.Scopes = make(map[*apiScope]scopeProperties)
1692 for _, apiScope := range allApiScopes {
1693 paths := sdk.getScopePaths(apiScope)
1694 jars := paths.stubsImplPath
1695 if len(jars) > 0 {
1696 properties := scopeProperties{}
1697 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01001698 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001699 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001700 properties.CurrentApiFile = paths.currentApiFilePath
1701 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001702 s.Scopes[apiScope] = properties
1703 }
1704 }
1705
1706 s.Libs = sdk.properties.Libs
Paul Duffinf7a64332020-05-13 16:54:55 +01001707 s.Naming_scheme = sdk.commonProperties.Naming_scheme
Paul Duffindd46f712020-02-10 13:37:10 +00001708}
1709
1710func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01001711 if s.Naming_scheme != nil {
1712 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
1713 }
1714
Paul Duffindd46f712020-02-10 13:37:10 +00001715 for _, apiScope := range allApiScopes {
1716 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01001717 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00001718
Paul Duffin3d1248c2020-04-09 00:10:17 +01001719 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1720
Paul Duffindd46f712020-02-10 13:37:10 +00001721 var jars []string
1722 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001723 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001724 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1725 jars = append(jars, dest)
1726 }
1727 scopeSet.AddProperty("jars", jars)
1728
Paul Duffin3d1248c2020-04-09 00:10:17 +01001729 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1730 // the source files are also unpacked.
1731 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1732 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1733 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1734
Paul Duffin1fd005d2020-04-09 01:08:11 +01001735 if properties.CurrentApiFile != nil {
1736 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1737 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1738 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1739 }
1740
1741 if properties.RemovedApiFile != nil {
1742 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1743 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1744 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1745 }
1746
Paul Duffindd46f712020-02-10 13:37:10 +00001747 if properties.SdkVersion != "" {
1748 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1749 }
1750 }
1751 }
1752
1753 if len(s.Libs) > 0 {
1754 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1755 }
1756}