blob: c91091ffcbd3698134b1ac47c05dd526169b8c9e [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 Duffin6a2bd112020-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 Duffin6a2bd112020-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 Duffin1c094a02020-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 Duffin5fb82132020-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 Duffin51a2bee2020-05-05 14:40:52 +010077 // The api scope that this scope extends.
78 extends *apiScope
79
Paul Duffin3a254982020-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 Duffin6a2bd112020-04-07 19:27:04 +010092 // The name of the field in the dynamically created structure.
93 fieldName string
94
Paul Duffin0f270632020-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 Duffina377e4c2020-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 Duffin5fb82132020-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 Duffin3c7c3472020-04-07 18:50:10 +0100121
122 // Extra arguments to pass to droidstubs for this scope.
123 droidstubsArgs []string
Anton Hansson5ff28e52020-05-02 11:19:36 +0100124
Paul Duffina377e4c2020-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 Hansson5ff28e52020-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 Duffin5fb82132020-04-29 20:45:27 +0100147 name := scope.name
Paul Duffin0f270632020-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 Duffin5fb82132020-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 Duffina377e4c2020-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 Duffin5fb82132020-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 Duffina377e4c2020-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 Duffinb74ee3e2020-05-08 14:16:20 +0100193func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100194 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000195}
196
Paul Duffin5fb82132020-04-29 20:45:27 +0100197func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100198 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000199}
200
Paul Duffina377e4c2020-04-29 13:30:54 +0100201func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100202 return baseName + ".api" + scope.moduleSuffix
Paul Duffina377e4c2020-04-29 13:30:54 +0100203}
204
Paul Duffin3a254982020-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 Duffin3a254982020-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 Duffin3a254982020-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 Hanssone366fff2020-04-28 16:47:41 +0100241 apiFilePrefix: "system-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100242 moduleSuffix: ".system",
Anton Hanssone366fff2020-04-28 16:47:41 +0100243 sdkVersion: "system_current",
Paul Duffin991f2622020-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 Duffin3a254982020-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 Hanssone366fff2020-04-28 16:47:41 +0100253 apiFilePrefix: "test-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100254 moduleSuffix: ".test",
Anton Hanssone366fff2020-04-28 16:47:41 +0100255 sdkVersion: "test_current",
256 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100257 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000258 })
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100259 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin0f270632020-05-13 19:19:49 +0100260 name: "module-lib",
Paul Duffin6d7f0a72020-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 Duffin6d7f0a72020-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 Duffin61871622020-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 Duffin3a254982020-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 Duffin080f5ee2020-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 Duffin3a254982020-04-28 10:44:03 +0100343}
344
Jiyong Parkc678ad32018-04-10 13:07:10 +0900345type sdkLibraryProperties struct {
Paul Duffin344c4ee2020-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 Duffin3a254982020-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 Duffin6d7f0a72020-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 Duffin8986cc92020-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 Duffin533f9c72020-05-20 16:18:00 +0100436// Paths to outputs from java_sdk_library and java_sdk_library_import.
437//
438// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
439// OptionalPaths are always set by java_sdk_library but may not be set by
440// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000441type scopePaths struct {
Paul Duffin533f9c72020-05-20 16:18:00 +0100442 // The path (represented as Paths for convenience when returning) to the stubs header jar.
443 //
444 // That is the jar that is created by turbine.
445 stubsHeaderPath android.Paths
446
447 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
448 //
449 // This is not the implementation jar, it still only contains stubs.
450 stubsImplPath android.Paths
451
452 // The API specification file, e.g. system_current.txt.
453 currentApiFilePath android.OptionalPath
454
455 // The specification of API elements removed since the last release.
456 removedApiFilePath android.OptionalPath
457
458 // The stubs source jar.
459 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000460}
461
Paul Duffin5fb82132020-04-29 20:45:27 +0100462func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
463 if lib, ok := dep.(Dependency); ok {
464 paths.stubsHeaderPath = lib.HeaderJars()
465 paths.stubsImplPath = lib.ImplementationJars()
466 return nil
467 } else {
468 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
469 }
470}
471
Paul Duffina377e4c2020-04-29 13:30:54 +0100472func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
473 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
474 action(apiStubsProvider)
Paul Duffin5fb82132020-04-29 20:45:27 +0100475 return nil
476 } else {
477 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
478 }
479}
480
Paul Duffin533f9c72020-05-20 16:18:00 +0100481func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
482 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
483 action(apiStubsProvider)
484 return nil
485 } else {
486 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
487 }
488}
489
Paul Duffina377e4c2020-04-29 13:30:54 +0100490func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin533f9c72020-05-20 16:18:00 +0100491 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
492 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffina377e4c2020-04-29 13:30:54 +0100493}
494
495func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
496 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
497 paths.extractApiInfoFromApiStubsProvider(provider)
498 })
499}
500
Paul Duffin533f9c72020-05-20 16:18:00 +0100501func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
502 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffina377e4c2020-04-29 13:30:54 +0100503}
504
505func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin533f9c72020-05-20 16:18:00 +0100506 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffina377e4c2020-04-29 13:30:54 +0100507 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
508 })
509}
510
511func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
512 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
513 paths.extractApiInfoFromApiStubsProvider(provider)
514 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
515 })
516}
517
518type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1a724e62020-05-08 13:44:43 +0100519 // The naming scheme to use for the components that this module creates.
520 //
Paul Duffindef8a892020-05-08 15:36:30 +0100521 // If not specified then it defaults to "default". The other allowable value is
522 // "framework-modules" which matches the scheme currently used by framework modules
523 // for the equivalent components represented as separate Soong modules.
Paul Duffin1a724e62020-05-08 13:44:43 +0100524 //
525 // This is a temporary mechanism to simplify conversion from separate modules for each
526 // component that follow a different naming pattern to the default one.
527 //
528 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffina377e4c2020-04-29 13:30:54 +0100529 Naming_scheme *string
530}
531
Paul Duffin56d44902020-01-31 13:36:25 +0000532// Common code between sdk library and sdk library import
533type commonToSdkLibraryAndImport struct {
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100534 moduleBase *android.ModuleBase
535
Paul Duffin56d44902020-01-31 13:36:25 +0000536 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100537
538 namingScheme sdkLibraryComponentNamingScheme
539
540 commonProperties commonToSdkLibraryAndImportProperties
Paul Duffin56d44902020-01-31 13:36:25 +0000541}
542
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100543func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
544 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100545
546 moduleBase.AddProperties(&c.commonProperties)
547}
548
549func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
550 schemeProperty := proptools.StringDefault(c.commonProperties.Naming_scheme, "default")
551 switch schemeProperty {
552 case "default":
553 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100554 case "framework-modules":
555 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100556 default:
557 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
558 return false
559 }
560
561 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100562}
563
564// Name of the java_library module that compiles the stubs source.
565func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100566 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100567}
568
569// Name of the droidstubs module that generates the stubs source and may also
570// generate/check the API.
571func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100572 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100573}
574
575// Name of the droidstubs module that generates/checks the API. Only used if it
576// requires different arts to the stubs source generating module.
577func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100578 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100579}
580
Paul Duffin5ae30792020-05-20 11:52:25 +0100581func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000582 if c.scopePaths == nil {
583 c.scopePaths = make(map[*apiScope]*scopePaths)
584 }
585 paths := c.scopePaths[scope]
586 if paths == nil {
587 paths = &scopePaths{}
588 c.scopePaths[scope] = paths
589 }
590
591 return paths
592}
593
Paul Duffin5ae30792020-05-20 11:52:25 +0100594func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
595 if c.scopePaths == nil {
596 return nil
597 }
598
599 return c.scopePaths[scope]
600}
601
602// If this does not support the requested api scope then find the closest available
603// scope it does support. Returns nil if no such scope is available.
604func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
605 for s := scope; s != nil; s = s.extends {
606 if paths := c.findScopePaths(s); paths != nil {
607 return paths
608 }
609 }
610
611 // This should never happen outside tests as public should be the base scope for every
612 // scope and is enabled by default.
613 return nil
614}
615
Paul Duffin47624362020-05-20 12:19:10 +0100616func (c *commonToSdkLibraryAndImport) sdkJarsCommon(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
617
618 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
619 if sdkVersion.version.isNumbered() {
620 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
621 }
622
623 var apiScope *apiScope
624 switch sdkVersion.kind {
625 case sdkSystem:
626 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100627 case sdkModule:
628 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100629 case sdkTest:
630 apiScope = apiScopeTest
631 default:
632 apiScope = apiScopePublic
633 }
634
Paul Duffin5ae30792020-05-20 11:52:25 +0100635 paths := c.findClosestScopePath(apiScope)
636 if paths == nil {
637 var scopes []string
638 for _, s := range allApiScopes {
639 if c.findScopePaths(s) != nil {
640 scopes = append(scopes, s.name)
641 }
642 }
643 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
644 return nil
645 }
646
Paul Duffin47624362020-05-20 12:19:10 +0100647 if headerJars {
648 return paths.stubsHeaderPath
649 } else {
650 return paths.stubsImplPath
651 }
652}
653
Inseob Kimc0907f12019-02-08 21:00:45 +0900654type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900655 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900656
Sundong Ahn054b19a2018-10-19 13:46:09 +0900657 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900658
Paul Duffin3a254982020-04-28 10:44:03 +0100659 // Map from api scope to the scope specific property structure.
660 scopeToProperties map[*apiScope]*ApiScopeProperties
661
Paul Duffin56d44902020-01-31 13:36:25 +0000662 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900663}
664
Inseob Kimc0907f12019-02-08 21:00:45 +0900665var _ Dependency = (*SdkLibrary)(nil)
666var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800667
Paul Duffin3a254982020-04-28 10:44:03 +0100668func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
669 return module.sdkLibraryProperties.Generate_system_and_test_apis
670}
671
672func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
673 // Check to see if any scopes have been explicitly enabled. If any have then all
674 // must be.
675 anyScopesExplicitlyEnabled := false
676 for _, scope := range allApiScopes {
677 scopeProperties := module.scopeToProperties[scope]
678 if scopeProperties.Enabled != nil {
679 anyScopesExplicitlyEnabled = true
680 break
681 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000682 }
Paul Duffin3a254982020-04-28 10:44:03 +0100683
684 var generatedScopes apiScopes
685 enabledScopes := make(map[*apiScope]struct{})
686 for _, scope := range allApiScopes {
687 scopeProperties := module.scopeToProperties[scope]
688 // If any scopes are explicitly enabled then ignore the legacy enabled status.
689 // This is to ensure that any new usages of this module type do not rely on legacy
690 // behaviour.
691 defaultEnabledStatus := false
692 if anyScopesExplicitlyEnabled {
693 defaultEnabledStatus = scope.defaultEnabledStatus
694 } else {
695 defaultEnabledStatus = scope.legacyEnabledStatus(module)
696 }
697 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
698 if enabled {
699 enabledScopes[scope] = struct{}{}
700 generatedScopes = append(generatedScopes, scope)
701 }
702 }
703
704 // Now check to make sure that any scope that is extended by an enabled scope is also
705 // enabled.
706 for _, scope := range allApiScopes {
707 if _, ok := enabledScopes[scope]; ok {
708 extends := scope.extends
709 if extends != nil {
710 if _, ok := enabledScopes[extends]; !ok {
711 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
712 }
713 }
714 }
715 }
716
717 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000718}
719
Paul Duffine74ac732020-02-06 13:51:46 +0000720var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
721
Jiyong Parke3833882020-02-17 17:28:10 +0900722func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
723 if dt, ok := depTag.(dependencyTag); ok {
724 return dt == xmlPermissionsFileTag
725 }
726 return false
727}
728
Inseob Kimc0907f12019-02-08 21:00:45 +0900729func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100730 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000731 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100732 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000733
Paul Duffina377e4c2020-04-29 13:30:54 +0100734 // If the stubs source and API cannot be generated together then add an additional dependency on
735 // the API module.
736 if apiScope.createStubsSourceAndApiTogether {
737 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100738 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100739 } else {
740 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100741 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
742 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100743 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900744 }
745
Paul Duffine74ac732020-02-06 13:51:46 +0000746 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
747 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900748 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000749 }
750
Sundong Ahn054b19a2018-10-19 13:46:09 +0900751 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900752}
753
Inseob Kimc0907f12019-02-08 21:00:45 +0900754func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000755 // Don't build an implementation library if this is api only.
756 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
757 module.Library.GenerateAndroidBuildActions(ctx)
758 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900759
Sundong Ahn57368eb2018-07-06 11:20:23 +0900760 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000761 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900762 // the recorded paths will be returned depending on the link type of the caller.
763 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900764 tag := ctx.OtherModuleDependencyTag(to)
765
Paul Duffin5fb82132020-04-29 20:45:27 +0100766 // Extract information from any of the scope specific dependencies.
767 if scopeTag, ok := tag.(scopeDependencyTag); ok {
768 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +0100769 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +0100770
771 // Extract information from the dependency. The exact information extracted
772 // is determined by the nature of the dependency which is determined by the tag.
773 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900774 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900775 })
776}
777
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900778func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000779 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
780 return nil
781 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900782 entriesList := module.Library.AndroidMkEntries()
783 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700784 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900785 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900786}
787
Jiyong Parkc678ad32018-04-10 13:07:10 +0900788// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900789func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900790 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900791}
792
Jiyong Parkc678ad32018-04-10 13:07:10 +0900793// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900794func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900795 return module.BaseModuleName() + sdkXmlFileSuffix
796}
797
Anton Hansson6bb88102020-03-27 19:43:19 +0000798// The dist path of the stub artifacts
799func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
800 if module.ModuleBase.Owner() != "" {
801 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
802 } else if Bool(module.sdkLibraryProperties.Core_lib) {
803 return path.Join("apistubs", "core", apiScope.name)
804 } else {
805 return path.Join("apistubs", "android", apiScope.name)
806 }
807}
808
Paul Duffin12ceb462019-12-24 20:31:31 +0000809// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +0100810func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +0100811 scopeProperties := module.scopeToProperties[apiScope]
812 if scopeProperties.Sdk_version != nil {
813 return proptools.String(scopeProperties.Sdk_version)
814 }
815
Paul Duffin12ceb462019-12-24 20:31:31 +0000816 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
817 if sdkDep.hasStandardLibs() {
818 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000819 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000820 } else {
821 // Otherwise, use no system module.
822 return "none"
823 }
824}
825
Paul Duffind1b3a922020-01-22 11:57:20 +0000826func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
827 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900828}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900829
Paul Duffind1b3a922020-01-22 11:57:20 +0000830func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
831 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900832}
833
834// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +0100835func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900836 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +0100837 Name *string
838 Visibility []string
839 Srcs []string
840 Installable *bool
841 Sdk_version *string
842 System_modules *string
843 Patch_module *string
844 Libs []string
845 Compile_dex *bool
846 Java_version *string
847 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900848 Pdk struct {
849 Enabled *bool
850 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900851 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900852 Openjdk9 struct {
853 Srcs []string
854 Javacflags []string
855 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000856 Dist struct {
857 Targets []string
858 Dest *string
859 Dir *string
860 Tag *string
861 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900862 }{}
863
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100864 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +0100865
866 // If stubs_library_visibility is not set then the created module will use the
867 // visibility of this module.
868 visibility := module.sdkLibraryProperties.Stubs_library_visibility
869 props.Visibility = visibility
870
Jiyong Parkc678ad32018-04-10 13:07:10 +0900871 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100872 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000873 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100874 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +0100875 props.System_modules = module.deviceProperties.System_modules
876 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000877 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900878 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssona9a31732020-05-21 10:38:30 +0100879 // The stub-annotations library contains special versions of the annotations
880 // with CLASS retention policy, so that they're kept around for kotlin.
881 props.Libs = append(props.Libs, "stub-annotations")
Jiyong Park82484c02018-04-23 21:41:26 +0900882 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +0100883 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
884 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
885 props.Java_version = module.properties.Java_version
886 if module.deviceProperties.Compile_dex != nil {
887 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900888 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900889
Anton Hansson6bb88102020-03-27 19:43:19 +0000890 // Dist the class jar artifact for sdk builds.
891 if !Bool(module.sdkLibraryProperties.No_dist) {
892 props.Dist.Targets = []string{"sdk", "win_sdk"}
893 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
894 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
895 props.Dist.Tag = proptools.StringPtr(".jar")
896 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900897
Colin Cross84dfc3d2019-09-25 11:33:01 -0700898 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900899}
900
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100901// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +0100902// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +0100903func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900904 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900905 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +0100906 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900907 Srcs []string
908 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100909 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000910 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900911 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000912 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900913 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900914 Java_version *string
Anton Hanssona9a31732020-05-21 10:38:30 +0100915 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +0900916 Merge_annotations_dirs []string
917 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +0100918 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +0900919 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900920 Current ApiToCheck
921 Last_released ApiToCheck
922 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +0100923
924 Api_lint struct {
925 Enabled *bool
926 New_since *string
927 Baseline_file *string
928 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900929 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900930 Aidl struct {
931 Include_dirs []string
932 Local_include_dirs []string
933 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000934 Dist struct {
935 Targets []string
936 Dest *string
937 Dir *string
938 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900939 }{}
940
Paul Duffinda364252020-04-28 14:08:32 +0100941 // The stubs source processing uses the same compile time classpath when extracting the
942 // API from the implementation library as it does when compiling it. i.e. the same
943 // * sdk version
944 // * system_modules
945 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100946
Paul Duffina377e4c2020-04-29 13:30:54 +0100947 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +0100948
949 // If stubs_source_visibility is not set then the created module will use the
950 // visibility of this module.
951 visibility := module.sdkLibraryProperties.Stubs_source_visibility
952 props.Visibility = visibility
953
Paul Duffinc5d954a2020-05-16 18:54:24 +0100954 props.Srcs = append(props.Srcs, module.properties.Srcs...)
955 props.Sdk_version = module.deviceProperties.Sdk_version
956 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900957 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900958 // A droiddoc module has only one Libs property and doesn't distinguish between
959 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +0100960 props.Libs = module.properties.Libs
961 props.Libs = append(props.Libs, module.properties.Static_libs...)
962 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
963 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
964 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900965
Anton Hanssona9a31732020-05-21 10:38:30 +0100966 props.Annotations_enabled = proptools.BoolPtr(true)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900967 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
968 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
969
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100970 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000971 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100972 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000973 }
974 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100975 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000976 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
977 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100978 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000979 disabledWarnings := []string{
980 "MissingPermission",
981 "BroadcastBehavior",
982 "HiddenSuperclass",
983 "DeprecationMismatch",
984 "UnavailableSymbol",
985 "SdkConstant",
986 "HiddenTypeParameter",
987 "Todo",
988 "Typo",
989 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100990 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900991
Paul Duffina377e4c2020-04-29 13:30:54 +0100992 if !createStubSources {
993 // Stubs are not required.
994 props.Generate_stubs = proptools.BoolPtr(false)
995 }
996
Paul Duffin3c7c3472020-04-07 18:50:10 +0100997 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +0100998 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000999 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001000 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001001
Paul Duffina377e4c2020-04-29 13:30:54 +01001002 if createApi {
1003 // List of APIs identified from the provided source files are created. They are later
1004 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1005 // last-released (a.k.a numbered) list of API.
1006 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1007 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1008 apiDir := module.getApiDir()
1009 currentApiFileName = path.Join(apiDir, currentApiFileName)
1010 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001011
Paul Duffina377e4c2020-04-29 13:30:54 +01001012 // check against the not-yet-release API
1013 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1014 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001015
Paul Duffina377e4c2020-04-29 13:30:54 +01001016 if !apiScope.unstable {
1017 // check against the latest released API
1018 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1019 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1020 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1021 module.latestRemovedApiFilegroupName(apiScope))
1022 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001023
Paul Duffina377e4c2020-04-29 13:30:54 +01001024 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1025 // Enable api lint.
1026 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1027 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001028
Paul Duffina377e4c2020-04-29 13:30:54 +01001029 // If it exists then pass a lint-baseline.txt through to droidstubs.
1030 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1031 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1032 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1033 if err != nil {
1034 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1035 }
1036 if len(paths) == 1 {
1037 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1038 } else if len(paths) != 0 {
1039 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1040 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001041 }
1042 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001043
Paul Duffina377e4c2020-04-29 13:30:54 +01001044 // Dist the api txt artifact for sdk builds.
1045 if !Bool(module.sdkLibraryProperties.No_dist) {
1046 props.Dist.Targets = []string{"sdk", "win_sdk"}
1047 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1048 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1049 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001050 }
1051
Colin Cross84dfc3d2019-09-25 11:33:01 -07001052 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001053}
1054
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001055func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1056 depTag := mctx.OtherModuleDependencyTag(dep)
1057 if depTag == xmlPermissionsFileTag {
1058 return true
1059 }
1060 return module.Library.DepIsInSameApex(mctx, dep)
1061}
1062
Jiyong Parkc678ad32018-04-10 13:07:10 +09001063// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001064func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001065 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001066 Name *string
1067 Lib_name *string
1068 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001069 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001070 Name: proptools.StringPtr(module.xmlFileName()),
1071 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1072 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001073 }
Jiyong Parke3833882020-02-17 17:28:10 +09001074
Jiyong Parke3833882020-02-17 17:28:10 +09001075 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001076}
1077
Paul Duffin50061512020-01-21 16:31:05 +00001078func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001079 var ver sdkVersion
1080 var kind sdkKind
1081 if s.usePrebuilt(ctx) {
1082 ver = s.version
1083 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001084 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001085 // We don't have prebuilt SDK for the specific sdkVersion.
1086 // Instead of breaking the build, fallback to use "system_current"
1087 ver = sdkVersionCurrent
1088 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001089 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001090
1091 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001092 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001093 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001094 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001095 if ctx.Config().AllowMissingDependencies() {
1096 return android.Paths{android.PathForSource(ctx, jar)}
1097 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001098 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001099 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001100 return nil
1101 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001102 return android.Paths{jarPath.Path()}
1103}
1104
Paul Duffin47624362020-05-20 12:19:10 +01001105func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001106
Paul Duffin47624362020-05-20 12:19:10 +01001107 // Check any special cases for java_sdk_library.
1108 if !sdkVersion.specified() {
Paul Duffind1b3a922020-01-22 11:57:20 +00001109 if headerJars {
Paul Duffin47624362020-05-20 12:19:10 +01001110 return module.HeaderJars()
Paul Duffind1b3a922020-01-22 11:57:20 +00001111 } else {
Paul Duffin47624362020-05-20 12:19:10 +01001112 return module.ImplementationJars()
Sundong Ahn054b19a2018-10-19 13:46:09 +09001113 }
Paul Duffin47624362020-05-20 12:19:10 +01001114 } else if sdkVersion.kind == sdkPrivate {
1115 return module.HeaderJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +09001116 }
Paul Duffin47624362020-05-20 12:19:10 +01001117
1118 return module.sdkJarsCommon(ctx, sdkVersion, headerJars)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001119}
1120
Sundong Ahn241cd372018-07-13 16:16:44 +09001121// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001122func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1123 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1124}
1125
1126// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001127func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001128 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001129}
1130
Sundong Ahn80a87b32019-05-13 15:02:50 +09001131func (module *SdkLibrary) SetNoDist() {
1132 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1133}
1134
Colin Cross571cccf2019-02-04 11:22:08 -08001135var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1136
Jiyong Park82484c02018-04-23 21:41:26 +09001137func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001138 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001139 return &[]string{}
1140 }).(*[]string)
1141}
1142
Paul Duffin749f98f2019-12-30 17:23:46 +00001143func (module *SdkLibrary) getApiDir() string {
1144 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1145}
1146
Jiyong Parkc678ad32018-04-10 13:07:10 +09001147// For a java_sdk_library module, create internal modules for stubs, docs,
1148// runtime libs and xml file. If requested, the stubs and docs are created twice
1149// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001150func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1151 // If the module has been disabled then don't create any child modules.
1152 if !module.Enabled() {
1153 return
1154 }
1155
Paul Duffinc5d954a2020-05-16 18:54:24 +01001156 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001157 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001158 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001159 }
1160
Paul Duffin37e0b772019-12-30 17:20:10 +00001161 // If this builds against standard libraries (i.e. is not part of the core libraries)
1162 // then assume it provides both system and test apis. Otherwise, assume it does not and
1163 // also assume it does not contribute to the dist build.
1164 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1165 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001166 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001167 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1168
Inseob Kim8098faa2019-03-18 10:19:51 +09001169 missing_current_api := false
1170
Paul Duffin3a254982020-04-28 10:44:03 +01001171 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001172
Paul Duffin749f98f2019-12-30 17:23:46 +00001173 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001174 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001175 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001176 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001177 p := android.ExistentPathForSource(mctx, path)
1178 if !p.Valid() {
1179 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1180 missing_current_api = true
1181 }
1182 }
1183 }
1184
1185 if missing_current_api {
1186 script := "build/soong/scripts/gen-java-current-api-files.sh"
1187 p := android.ExistentPathForSource(mctx, script)
1188
1189 if !p.Valid() {
1190 panic(fmt.Sprintf("script file %s doesn't exist", script))
1191 }
1192
1193 mctx.ModuleErrorf("One or more current api files are missing. "+
1194 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001195 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001196 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001197 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001198 return
1199 }
1200
Paul Duffin3a254982020-04-28 10:44:03 +01001201 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001202 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001203 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001204
1205 // If the args needed to generate the stubs and API are the same then they
1206 // can be generated in a single invocation of metalava, otherwise they will
1207 // need separate invocations.
1208 if scope.createStubsSourceAndApiTogether {
1209 // Use the stubs source name for legacy reasons.
1210 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1211 } else {
1212 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1213
1214 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001215 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001216 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1217 }
1218
Paul Duffind1b3a922020-01-22 11:57:20 +00001219 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001220 }
1221
Paul Duffin43db9be2019-12-30 17:35:49 +00001222 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
1223 // for runtime
1224 module.createXmlFile(mctx)
1225
1226 // record java_sdk_library modules so that they are exported to make
1227 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1228 javaSdkLibrariesLock.Lock()
1229 defer javaSdkLibrariesLock.Unlock()
1230 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1231 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001232}
1233
1234func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001235 module.AddProperties(
1236 &module.sdkLibraryProperties,
Paul Duffinc5d954a2020-05-16 18:54:24 +01001237 &module.properties,
1238 &module.dexpreoptProperties,
1239 &module.deviceProperties,
1240 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001241 )
1242
Paul Duffinc5d954a2020-05-16 18:54:24 +01001243 module.properties.Installable = proptools.BoolPtr(true)
1244 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001245}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001246
Paul Duffin1a724e62020-05-08 13:44:43 +01001247// Defines how to name the individual component modules the sdk library creates.
1248type sdkLibraryComponentNamingScheme interface {
1249 stubsLibraryModuleName(scope *apiScope, baseName string) string
1250
1251 stubsSourceModuleName(scope *apiScope, baseName string) string
1252
1253 apiModuleName(scope *apiScope, baseName string) string
1254}
1255
1256type defaultNamingScheme struct {
1257}
1258
1259func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1260 return scope.stubsLibraryModuleName(baseName)
1261}
1262
1263func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1264 return scope.stubsSourceModuleName(baseName)
1265}
1266
1267func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1268 return scope.apiModuleName(baseName)
1269}
1270
1271var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1272
Paul Duffindef8a892020-05-08 15:36:30 +01001273type frameworkModulesNamingScheme struct {
1274}
1275
1276func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1277 suffix := scope.name
1278 if scope == apiScopeModuleLib {
1279 suffix = "module_libs_"
1280 }
1281 return suffix
1282}
1283
1284func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1285 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1286}
1287
1288func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1289 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1290}
1291
1292func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1293 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1294}
1295
1296var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1297
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001298// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1299// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1300// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1301// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1302// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001303func SdkLibraryFactory() android.Module {
1304 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001305
1306 // Initialize information common between source and prebuilt.
1307 module.initCommon(&module.ModuleBase)
1308
Inseob Kimc0907f12019-02-08 21:00:45 +09001309 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001310 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001311 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001312
1313 // Initialize the map from scope to scope specific properties.
1314 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1315 for _, scope := range allApiScopes {
1316 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1317 }
1318 module.scopeToProperties = scopeToProperties
1319
Paul Duffin344c4ee2020-04-29 23:35:13 +01001320 // Add the properties containing visibility rules so that they are checked.
1321 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1322 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1323
Paul Duffin1a724e62020-05-08 13:44:43 +01001324 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
1325 if module.initCommonAfterDefaultsApplied(ctx) {
1326 module.CreateInternalModules(ctx)
1327 }
1328 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001329 return module
1330}
Colin Cross79c7c262019-04-17 11:11:46 -07001331
1332//
1333// SDK library prebuilts
1334//
1335
Paul Duffin56d44902020-01-31 13:36:25 +00001336// Properties associated with each api scope.
1337type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001338 Jars []string `android:"path"`
1339
1340 Sdk_version *string
1341
Colin Cross79c7c262019-04-17 11:11:46 -07001342 // List of shared java libs that this module has dependencies to
1343 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001344
Paul Duffin5fb82132020-04-29 20:45:27 +01001345 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001346 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001347
1348 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001349 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001350
1351 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001352 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001353}
1354
Paul Duffin56d44902020-01-31 13:36:25 +00001355type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001356 // List of shared java libs, common to all scopes, that this module has
1357 // dependencies to
1358 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001359}
1360
Colin Cross79c7c262019-04-17 11:11:46 -07001361type sdkLibraryImport struct {
1362 android.ModuleBase
1363 android.DefaultableModuleBase
1364 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001365 android.ApexModuleBase
1366 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001367
1368 properties sdkLibraryImportProperties
1369
Paul Duffin6a2bd112020-04-07 19:27:04 +01001370 // Map from api scope to the scope specific property structure.
1371 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1372
Paul Duffin56d44902020-01-31 13:36:25 +00001373 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001374}
1375
1376var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1377
Paul Duffin6a2bd112020-04-07 19:27:04 +01001378// The type of a structure that contains a field of type sdkLibraryScopeProperties
1379// for each apiscope in allApiScopes, e.g. something like:
1380// struct {
1381// Public sdkLibraryScopeProperties
1382// System sdkLibraryScopeProperties
1383// ...
1384// }
1385var allScopeStructType = createAllScopePropertiesStructType()
1386
1387// Dynamically create a structure type for each apiscope in allApiScopes.
1388func createAllScopePropertiesStructType() reflect.Type {
1389 var fields []reflect.StructField
1390 for _, apiScope := range allApiScopes {
1391 field := reflect.StructField{
1392 Name: apiScope.fieldName,
1393 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1394 }
1395 fields = append(fields, field)
1396 }
1397
1398 return reflect.StructOf(fields)
1399}
1400
1401// Create an instance of the scope specific structure type and return a map
1402// from apiscope to a pointer to each scope specific field.
1403func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1404 allScopePropertiesPtr := reflect.New(allScopeStructType)
1405 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1406 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1407
1408 for _, apiScope := range allApiScopes {
1409 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1410 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1411 }
1412
1413 return allScopePropertiesPtr.Interface(), scopeProperties
1414}
1415
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001416// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001417func sdkLibraryImportFactory() android.Module {
1418 module := &sdkLibraryImport{}
1419
Paul Duffin6a2bd112020-04-07 19:27:04 +01001420 allScopeProperties, scopeToProperties := createPropertiesInstance()
1421 module.scopeProperties = scopeToProperties
1422 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001423
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001424 // Initialize information common between source and prebuilt.
1425 module.initCommon(&module.ModuleBase)
1426
Paul Duffin0bdcb272020-02-06 15:24:57 +00001427 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001428 android.InitApexModule(module)
1429 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001430 InitJavaModule(module, android.HostAndDeviceSupported)
1431
Paul Duffin1a724e62020-05-08 13:44:43 +01001432 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1433 if module.initCommonAfterDefaultsApplied(mctx) {
1434 module.createInternalModules(mctx)
1435 }
1436 })
Colin Cross79c7c262019-04-17 11:11:46 -07001437 return module
1438}
1439
1440func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1441 return &module.prebuilt
1442}
1443
1444func (module *sdkLibraryImport) Name() string {
1445 return module.prebuilt.Name(module.ModuleBase.Name())
1446}
1447
Paul Duffinbf735aa2020-05-08 15:01:19 +01001448func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001449
Paul Duffin50061512020-01-21 16:31:05 +00001450 // If the build is configured to use prebuilts then force this to be preferred.
1451 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1452 module.prebuilt.ForcePrefer()
1453 }
1454
Paul Duffin6a2bd112020-04-07 19:27:04 +01001455 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001456 if len(scopeProperties.Jars) == 0 {
1457 continue
1458 }
1459
Paul Duffinf6155722020-04-09 00:07:11 +01001460 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001461
Paul Duffin533f9c72020-05-20 16:18:00 +01001462 if len(scopeProperties.Stub_srcs) > 0 {
1463 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1464 }
Paul Duffin56d44902020-01-31 13:36:25 +00001465 }
Colin Cross79c7c262019-04-17 11:11:46 -07001466
1467 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1468 javaSdkLibrariesLock.Lock()
1469 defer javaSdkLibrariesLock.Unlock()
1470 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1471}
1472
Paul Duffinbf735aa2020-05-08 15:01:19 +01001473func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001474 // Creates a java import for the jar with ".stubs" suffix
1475 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001476 Name *string
1477 Sdk_version *string
1478 Libs []string
1479 Jars []string
1480 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001481 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001482 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001483 props.Sdk_version = scopeProperties.Sdk_version
1484 // Prepend any of the libs from the legacy public properties to the libs for each of the
1485 // scopes to avoid having to duplicate them in each scope.
1486 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1487 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001488
Paul Duffindd89a282020-05-13 16:08:09 +01001489 // The imports are preferred if the java_sdk_library_import is preferred.
1490 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf6155722020-04-09 00:07:11 +01001491 mctx.CreateModule(ImportFactory, &props)
1492}
1493
Paul Duffinbf735aa2020-05-08 15:01:19 +01001494func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001495 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001496 Name *string
1497 Srcs []string
1498 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001499 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001500 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001501 props.Srcs = scopeProperties.Stub_srcs
1502 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001503
1504 // The stubs source is preferred if the java_sdk_library_import is preferred.
1505 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001506}
1507
Colin Cross79c7c262019-04-17 11:11:46 -07001508func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001509 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001510 if len(scopeProperties.Jars) == 0 {
1511 continue
1512 }
1513
1514 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001515 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001516
1517 if len(scopeProperties.Stub_srcs) > 0 {
1518 // Add dependencies to the prebuilt stubs source library
1519 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1520 }
Paul Duffin56d44902020-01-31 13:36:25 +00001521 }
Colin Cross79c7c262019-04-17 11:11:46 -07001522}
1523
1524func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001525 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001526 ctx.VisitDirectDeps(func(to android.Module) {
1527 tag := ctx.OtherModuleDependencyTag(to)
1528
Paul Duffin533f9c72020-05-20 16:18:00 +01001529 // Extract information from any of the scope specific dependencies.
1530 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1531 apiScope := scopeTag.apiScope
1532 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1533
1534 // Extract information from the dependency. The exact information extracted
1535 // is determined by the nature of the dependency which is determined by the tag.
1536 scopeTag.extractDepInfo(ctx, to, scopePaths)
Colin Cross79c7c262019-04-17 11:11:46 -07001537 }
1538 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001539
1540 // Populate the scope paths with information from the properties.
1541 for apiScope, scopeProperties := range module.scopeProperties {
1542 if len(scopeProperties.Jars) == 0 {
1543 continue
1544 }
1545
1546 paths := module.getScopePathsCreateIfNeeded(apiScope)
1547 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1548 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1549 }
Colin Cross79c7c262019-04-17 11:11:46 -07001550}
1551
Paul Duffin47624362020-05-20 12:19:10 +01001552func (module *sdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin56d44902020-01-31 13:36:25 +00001553
Paul Duffin47624362020-05-20 12:19:10 +01001554 // The java_sdk_library_import can only ever give back header jars as it does not
1555 // have an implementation jar.
1556 headerJars := true
1557 return module.sdkJarsCommon(ctx, sdkVersion, headerJars)
Paul Duffin56d44902020-01-31 13:36:25 +00001558}
1559
Colin Cross79c7c262019-04-17 11:11:46 -07001560// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001561func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001562 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001563 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001564}
1565
1566// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001567func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001568 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001569 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001570}
Jiyong Parke3833882020-02-17 17:28:10 +09001571
1572//
1573// java_sdk_library_xml
1574//
1575type sdkLibraryXml struct {
1576 android.ModuleBase
1577 android.DefaultableModuleBase
1578 android.ApexModuleBase
1579
1580 properties sdkLibraryXmlProperties
1581
1582 outputFilePath android.OutputPath
1583 installDirPath android.InstallPath
1584}
1585
1586type sdkLibraryXmlProperties struct {
1587 // canonical name of the lib
1588 Lib_name *string
1589}
1590
1591// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1592// Not to be used directly by users. java_sdk_library internally uses this.
1593func sdkLibraryXmlFactory() android.Module {
1594 module := &sdkLibraryXml{}
1595
1596 module.AddProperties(&module.properties)
1597
1598 android.InitApexModule(module)
1599 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1600
1601 return module
1602}
1603
1604// from android.PrebuiltEtcModule
1605func (module *sdkLibraryXml) SubDir() string {
1606 return "permissions"
1607}
1608
1609// from android.PrebuiltEtcModule
1610func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1611 return module.outputFilePath
1612}
1613
1614// from android.ApexModule
1615func (module *sdkLibraryXml) AvailableFor(what string) bool {
1616 return true
1617}
1618
1619func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1620 // do nothing
1621}
1622
1623// File path to the runtime implementation library
1624func (module *sdkLibraryXml) implPath() string {
1625 implName := proptools.String(module.properties.Lib_name)
1626 if apexName := module.ApexName(); apexName != "" {
1627 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1628 // In most cases, this works fine. But when apex_name is set or override_apex is used
1629 // this can be wrong.
1630 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1631 }
1632 partition := "system"
1633 if module.SocSpecific() {
1634 partition = "vendor"
1635 } else if module.DeviceSpecific() {
1636 partition = "odm"
1637 } else if module.ProductSpecific() {
1638 partition = "product"
1639 } else if module.SystemExtSpecific() {
1640 partition = "system_ext"
1641 }
1642 return "/" + partition + "/framework/" + implName + ".jar"
1643}
1644
1645func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1646 libName := proptools.String(module.properties.Lib_name)
1647 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1648
1649 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1650 rule := android.NewRuleBuilder()
1651 rule.Command().
1652 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1653 Output(module.outputFilePath)
1654
1655 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1656
1657 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1658}
1659
1660func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1661 if !module.IsForPlatform() {
1662 return []android.AndroidMkEntries{android.AndroidMkEntries{
1663 Disabled: true,
1664 }}
1665 }
1666
1667 return []android.AndroidMkEntries{android.AndroidMkEntries{
1668 Class: "ETC",
1669 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1670 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1671 func(entries *android.AndroidMkEntries) {
1672 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1673 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1674 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1675 },
1676 },
1677 }}
1678}
Paul Duffin61871622020-02-10 13:37:10 +00001679
1680type sdkLibrarySdkMemberType struct {
1681 android.SdkMemberTypeBase
1682}
1683
1684func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1685 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1686}
1687
1688func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1689 _, ok := module.(*SdkLibrary)
1690 return ok
1691}
1692
1693func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1694 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1695}
1696
1697func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1698 return &sdkLibrarySdkMemberProperties{}
1699}
1700
1701type sdkLibrarySdkMemberProperties struct {
1702 android.SdkMemberPropertiesBase
1703
1704 // Scope to per scope properties.
1705 Scopes map[*apiScope]scopeProperties
1706
1707 // Additional libraries that the exported stubs libraries depend upon.
1708 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001709
1710 // The Java stubs source files.
1711 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01001712
1713 // The naming scheme.
1714 Naming_scheme *string
Paul Duffin61871622020-02-10 13:37:10 +00001715}
1716
1717type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01001718 Jars android.Paths
1719 StubsSrcJar android.Path
1720 CurrentApiFile android.Path
1721 RemovedApiFile android.Path
1722 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00001723}
1724
1725func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1726 sdk := variant.(*SdkLibrary)
1727
1728 s.Scopes = make(map[*apiScope]scopeProperties)
1729 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01001730 paths := sdk.findScopePaths(apiScope)
1731 if paths == nil {
1732 continue
1733 }
1734
Paul Duffin61871622020-02-10 13:37:10 +00001735 jars := paths.stubsImplPath
1736 if len(jars) > 0 {
1737 properties := scopeProperties{}
1738 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01001739 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01001740 properties.StubsSrcJar = paths.stubsSrcJar.Path()
1741 properties.CurrentApiFile = paths.currentApiFilePath.Path()
1742 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffin61871622020-02-10 13:37:10 +00001743 s.Scopes[apiScope] = properties
1744 }
1745 }
1746
1747 s.Libs = sdk.properties.Libs
Paul Duffinf8e08b22020-05-13 16:54:55 +01001748 s.Naming_scheme = sdk.commonProperties.Naming_scheme
Paul Duffin61871622020-02-10 13:37:10 +00001749}
1750
1751func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01001752 if s.Naming_scheme != nil {
1753 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
1754 }
1755
Paul Duffin61871622020-02-10 13:37:10 +00001756 for _, apiScope := range allApiScopes {
1757 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01001758 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00001759
Paul Duffinf488ef22020-04-09 00:10:17 +01001760 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1761
Paul Duffin61871622020-02-10 13:37:10 +00001762 var jars []string
1763 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01001764 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00001765 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1766 jars = append(jars, dest)
1767 }
1768 scopeSet.AddProperty("jars", jars)
1769
Paul Duffinf488ef22020-04-09 00:10:17 +01001770 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1771 // the source files are also unpacked.
1772 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1773 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1774 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1775
Paul Duffin75dcc802020-04-09 01:08:11 +01001776 if properties.CurrentApiFile != nil {
1777 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1778 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1779 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1780 }
1781
1782 if properties.RemovedApiFile != nil {
1783 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1784 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1785 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1786 }
1787
Paul Duffin61871622020-02-10 13:37:10 +00001788 if properties.SdkVersion != "" {
1789 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1790 }
1791 }
1792 }
1793
1794 if len(s.Libs) > 0 {
1795 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1796 }
1797}