blob: dfda6120860f47f446c101c468af978a5365db08 [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 (
Jiyong Parkc678ad32018-04-10 13:07:10 +090033 sdkStubsLibrarySuffix = ".stubs"
34 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090035 sdkTestApiSuffix = ".test"
Paul Duffin91b883d2020-02-11 13:05:28 +000036 sdkStubsSourceSuffix = ".stubs.source"
Paul Duffin0ff08bd2020-04-29 13:30:54 +010037 sdkApiSuffix = ".api"
Jiyong Parkc678ad32018-04-10 13:07:10 +090038 sdkXmlFileSuffix = ".xml"
Jiyong Parke3833882020-02-17 17:28:10 +090039 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090040 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
41 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090042 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090043 ` you may not use this file except in compliance with the License.\n` +
44 ` You may obtain a copy of the License at\n` +
45 `\n` +
46 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
47 `\n` +
48 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090049 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090050 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
51 ` See the License for the specific language governing permissions and\n` +
52 ` limitations under the License.\n` +
53 `-->\n` +
54 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090055 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090056 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090057)
58
Paul Duffind1b3a922020-01-22 11:57:20 +000059// A tag to associated a dependency with a specific api scope.
60type scopeDependencyTag struct {
61 blueprint.BaseDependencyTag
62 name string
63 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010064
65 // Function for extracting appropriate path information from the dependency.
66 depInfoExtractor func(paths *scopePaths, dep android.Module) error
67}
68
69// Extract tag specific information from the dependency.
70func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
71 err := tag.depInfoExtractor(paths, dep)
72 if err != nil {
73 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
74 }
Paul Duffind1b3a922020-01-22 11:57:20 +000075}
76
77// Provides information about an api scope, e.g. public, system, test.
78type apiScope struct {
79 // The name of the api scope, e.g. public, system, test
80 name string
81
Paul Duffin97b53b82020-05-05 14:40:52 +010082 // The api scope that this scope extends.
83 extends *apiScope
84
Paul Duffin3375e352020-04-28 10:44:03 +010085 // The legacy enabled status for a specific scope can be dependent on other
86 // properties that have been specified on the library so it is provided by
87 // a function that can determine the status by examining those properties.
88 legacyEnabledStatus func(module *SdkLibrary) bool
89
90 // The default enabled status for non-legacy behavior, which is triggered by
91 // explicitly enabling at least one api scope.
92 defaultEnabledStatus bool
93
94 // Gets a pointer to the scope specific properties.
95 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
96
Paul Duffin46a26a82020-04-07 19:27:04 +010097 // The name of the field in the dynamically created structure.
98 fieldName string
99
Paul Duffind1b3a922020-01-22 11:57:20 +0000100 // The tag to use to depend on the stubs library module.
101 stubsTag scopeDependencyTag
102
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100103 // The tag to use to depend on the stubs source module (if separate from the API module).
104 stubsSourceTag scopeDependencyTag
105
106 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
107 apiFileTag scopeDependencyTag
108
Paul Duffinc8782502020-04-29 20:45:27 +0100109 // The tag to use to depend on the stubs source and API module.
110 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000111
112 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
113 apiFilePrefix string
114
115 // The scope specific prefix to add to the sdk library module name to construct a scope specific
116 // module name.
117 moduleSuffix string
118
Paul Duffind1b3a922020-01-22 11:57:20 +0000119 // SDK version that the stubs library is built against. Note that this is always
120 // *current. Older stubs library built with a numbered SDK version is created from
121 // the prebuilt jar.
122 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100123
124 // Extra arguments to pass to droidstubs for this scope.
125 droidstubsArgs []string
Anton Hansson6478ac12020-05-02 11:19:36 +0100126
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100127 // The args that must be passed to droidstubs to generate the stubs source
128 // for this scope.
129 //
130 // The stubs source must include the definitions of everything that is in this
131 // api scope and all the scopes that this one extends.
132 droidstubsArgsForGeneratingStubsSource []string
133
134 // The args that must be passed to droidstubs to generate the API for this scope.
135 //
136 // The API only includes the additional members that this scope adds over the scope
137 // that it extends.
138 droidstubsArgsForGeneratingApi []string
139
140 // True if the stubs source and api can be created by the same metalava invocation.
141 createStubsSourceAndApiTogether bool
142
Anton Hansson6478ac12020-05-02 11:19:36 +0100143 // Whether the api scope can be treated as unstable, and should skip compat checks.
144 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000145}
146
147// Initialize a scope, creating and adding appropriate dependency tags
148func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100149 name := scope.name
150 scope.fieldName = proptools.FieldNameForProperty(name)
Paul Duffind1b3a922020-01-22 11:57:20 +0000151 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100152 name: name + "-stubs",
153 apiScope: scope,
154 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000155 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100156 scope.stubsSourceTag = scopeDependencyTag{
157 name: name + "-stubs-source",
158 apiScope: scope,
159 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
160 }
161 scope.apiFileTag = scopeDependencyTag{
162 name: name + "-api",
163 apiScope: scope,
164 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
165 }
Paul Duffinc8782502020-04-29 20:45:27 +0100166 scope.stubsSourceAndApiTag = scopeDependencyTag{
167 name: name + "-stubs-source-and-api",
168 apiScope: scope,
169 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000170 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100171
172 // To get the args needed to generate the stubs source append all the args from
173 // this scope and all the scopes it extends as each set of args adds additional
174 // members to the stubs.
175 var stubsSourceArgs []string
176 for s := scope; s != nil; s = s.extends {
177 stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
178 }
179 scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
180
181 // Currently the args needed to generate the API are the same as the args
182 // needed to add additional members.
183 apiArgs := scope.droidstubsArgs
184 scope.droidstubsArgsForGeneratingApi = apiArgs
185
186 // If the args needed to generate the stubs and API are the same then they
187 // can be generated in a single invocation of metalava, otherwise they will
188 // need separate invocations.
189 scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
190
Paul Duffind1b3a922020-01-22 11:57:20 +0000191 return scope
192}
193
Paul Duffinc3091c82020-05-08 14:16:20 +0100194func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffind1b3a922020-01-22 11:57:20 +0000195 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
196}
197
Paul Duffinc8782502020-04-29 20:45:27 +0100198func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000199 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000200}
201
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100202func (scope *apiScope) apiModuleName(baseName string) string {
203 return baseName + sdkApiSuffix + scope.moduleSuffix
204}
205
Paul Duffin3375e352020-04-28 10:44:03 +0100206func (scope *apiScope) String() string {
207 return scope.name
208}
209
Paul Duffind1b3a922020-01-22 11:57:20 +0000210type apiScopes []*apiScope
211
212func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
213 var list []string
214 for _, scope := range scopes {
215 list = append(list, accessor(scope))
216 }
217 return list
218}
219
Jiyong Parkc678ad32018-04-10 13:07:10 +0900220var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000221 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100222 name: "public",
223
224 // Public scope is enabled by default for both legacy and non-legacy modes.
225 legacyEnabledStatus: func(module *SdkLibrary) bool {
226 return true
227 },
228 defaultEnabledStatus: true,
229
230 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
231 return &module.sdkLibraryProperties.Public
232 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000233 sdkVersion: "current",
234 })
235 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100236 name: "system",
237 extends: apiScopePublic,
238 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
239 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
240 return &module.sdkLibraryProperties.System
241 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100242 apiFilePrefix: "system-",
243 moduleSuffix: sdkSystemApiSuffix,
244 sdkVersion: "system_current",
Paul Duffin0d543642020-04-29 22:18:41 +0100245 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000246 })
247 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100248 name: "test",
249 extends: apiScopePublic,
250 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
251 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
252 return &module.sdkLibraryProperties.Test
253 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100254 apiFilePrefix: "test-",
255 moduleSuffix: sdkTestApiSuffix,
256 sdkVersion: "test_current",
257 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson6478ac12020-05-02 11:19:36 +0100258 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000259 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100260 apiScopeModuleLib = initApiScope(&apiScope{
261 name: "module_lib",
262 extends: apiScopeSystem,
263 // Module_lib scope is disabled by default in legacy mode.
264 //
265 // Enabling this would break existing usages.
266 legacyEnabledStatus: func(module *SdkLibrary) bool {
267 return false
268 },
269 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
270 return &module.sdkLibraryProperties.Module_lib
271 },
272 apiFilePrefix: "module-lib-",
273 moduleSuffix: ".module_lib",
274 sdkVersion: "module_current",
275 droidstubsArgs: []string{
276 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
277 },
278 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000279 allApiScopes = apiScopes{
280 apiScopePublic,
281 apiScopeSystem,
282 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100283 apiScopeModuleLib,
Paul Duffind1b3a922020-01-22 11:57:20 +0000284 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900285)
286
Jiyong Park82484c02018-04-23 21:41:26 +0900287var (
288 javaSdkLibrariesLock sync.Mutex
289)
290
Jiyong Parkc678ad32018-04-10 13:07:10 +0900291// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900292// 1) disallowing linking to the runtime shared lib
293// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900294
295func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000296 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900297
Jiyong Park82484c02018-04-23 21:41:26 +0900298 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
299 javaSdkLibraries := javaSdkLibraries(ctx.Config())
300 sort.Strings(*javaSdkLibraries)
301 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
302 })
Paul Duffindd46f712020-02-10 13:37:10 +0000303
304 // Register sdk member types.
305 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
306 android.SdkMemberTypeBase{
307 PropertyName: "java_sdk_libs",
308 SupportsSdk: true,
309 },
310 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900311}
312
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000313func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
314 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
315 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
316}
317
Paul Duffin3375e352020-04-28 10:44:03 +0100318// Properties associated with each api scope.
319type ApiScopeProperties struct {
320 // Indicates whether the api surface is generated.
321 //
322 // If this is set for any scope then all scopes must explicitly specify if they
323 // are enabled. This is to prevent new usages from depending on legacy behavior.
324 //
325 // Otherwise, if this is not set for any scope then the default behavior is
326 // scope specific so please refer to the scope specific property documentation.
327 Enabled *bool
328}
329
Jiyong Parkc678ad32018-04-10 13:07:10 +0900330type sdkLibraryProperties struct {
Paul Duffin4911a892020-04-29 23:35:13 +0100331 // Visibility for stubs library modules. If not specified then defaults to the
332 // visibility property.
333 Stubs_library_visibility []string
334
335 // Visibility for stubs source modules. If not specified then defaults to the
336 // visibility property.
337 Stubs_source_visibility []string
338
Sundong Ahnf043cf62018-06-25 16:04:37 +0900339 // List of Java libraries that will be in the classpath when building stubs
340 Stub_only_libs []string `android:"arch_variant"`
341
Paul Duffin7a586d32019-12-30 17:09:34 +0000342 // list of package names that will be documented and publicized as API.
343 // This allows the API to be restricted to a subset of the source files provided.
344 // If this is unspecified then all the source files will be treated as being part
345 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900346 Api_packages []string
347
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900348 // list of package names that must be hidden from the API
349 Hidden_api_packages []string
350
Paul Duffin749f98f2019-12-30 17:23:46 +0000351 // the relative path to the directory containing the api specification files.
352 // Defaults to "api".
353 Api_dir *string
354
Paul Duffin43db9be2019-12-30 17:35:49 +0000355 // If set to true there is no runtime library.
356 Api_only *bool
357
Paul Duffin11512472019-02-11 15:55:17 +0000358 // local files that are used within user customized droiddoc options.
359 Droiddoc_option_files []string
360
361 // additional droiddoc options
362 // Available variables for substitution:
363 //
364 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900365 Droiddoc_options []string
366
Sundong Ahn054b19a2018-10-19 13:46:09 +0900367 // a list of top-level directories containing files to merge qualifier annotations
368 // (i.e. those intended to be included in the stubs written) from.
369 Merge_annotations_dirs []string
370
371 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
372 Merge_inclusion_annotations_dirs []string
373
374 // If set to true, the path of dist files is apistubs/core. Defaults to false.
375 Core_lib *bool
376
Sundong Ahn80a87b32019-05-13 15:02:50 +0900377 // don't create dist rules.
378 No_dist *bool `blueprint:"mutated"`
379
Paul Duffin3375e352020-04-28 10:44:03 +0100380 // indicates whether system and test apis should be generated.
381 Generate_system_and_test_apis bool `blueprint:"mutated"`
382
383 // The properties specific to the public api scope
384 //
385 // Unless explicitly specified by using public.enabled the public api scope is
386 // enabled by default in both legacy and non-legacy mode.
387 Public ApiScopeProperties
388
389 // The properties specific to the system api scope
390 //
391 // In legacy mode the system api scope is enabled by default when sdk_version
392 // is set to something other than "none".
393 //
394 // In non-legacy mode the system api scope is disabled by default.
395 System ApiScopeProperties
396
397 // The properties specific to the test api scope
398 //
399 // In legacy mode the test api scope is enabled by default when sdk_version
400 // is set to something other than "none".
401 //
402 // In non-legacy mode the test api scope is disabled by default.
403 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000404
Paul Duffin8f265b92020-04-28 14:13:56 +0100405 // The properties specific to the module_lib api scope
406 //
407 // Unless explicitly specified by using test.enabled the module_lib api scope is
408 // disabled by default.
409 Module_lib ApiScopeProperties
410
Paul Duffin160fe412020-05-10 19:32:20 +0100411 // Properties related to api linting.
412 Api_lint struct {
413 // Enable api linting.
414 Enabled *bool
415 }
416
Jiyong Parkc678ad32018-04-10 13:07:10 +0900417 // TODO: determines whether to create HTML doc or not
418 //Html_doc *bool
419}
420
Paul Duffind1b3a922020-01-22 11:57:20 +0000421type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100422 stubsHeaderPath android.Paths
423 stubsImplPath android.Paths
424 currentApiFilePath android.Path
425 removedApiFilePath android.Path
426 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000427}
428
Paul Duffinc8782502020-04-29 20:45:27 +0100429func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
430 if lib, ok := dep.(Dependency); ok {
431 paths.stubsHeaderPath = lib.HeaderJars()
432 paths.stubsImplPath = lib.ImplementationJars()
433 return nil
434 } else {
435 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
436 }
437}
438
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100439func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
440 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
441 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100442 return nil
443 } else {
444 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
445 }
446}
447
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100448func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
449 paths.currentApiFilePath = provider.ApiFilePath()
450 paths.removedApiFilePath = provider.RemovedApiFilePath()
451}
452
453func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
454 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
455 paths.extractApiInfoFromApiStubsProvider(provider)
456 })
457}
458
459func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsProvider) {
460 paths.stubsSrcJar = provider.StubsSrcJar()
461}
462
463func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
464 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
465 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
466 })
467}
468
469func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
470 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
471 paths.extractApiInfoFromApiStubsProvider(provider)
472 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
473 })
474}
475
476type commonToSdkLibraryAndImportProperties struct {
477 Naming_scheme *string
478}
479
Paul Duffin56d44902020-01-31 13:36:25 +0000480// Common code between sdk library and sdk library import
481type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100482 moduleBase *android.ModuleBase
483
Paul Duffin56d44902020-01-31 13:36:25 +0000484 scopePaths map[*apiScope]*scopePaths
485}
486
Paul Duffinc3091c82020-05-08 14:16:20 +0100487func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
488 c.moduleBase = moduleBase
489}
490
491// Name of the java_library module that compiles the stubs source.
492func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
493 return apiScope.stubsLibraryModuleName(c.moduleBase.BaseModuleName())
494}
495
496// Name of the droidstubs module that generates the stubs source and may also
497// generate/check the API.
498func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
499 return apiScope.stubsSourceModuleName(c.moduleBase.BaseModuleName())
500}
501
502// Name of the droidstubs module that generates/checks the API. Only used if it
503// requires different arts to the stubs source generating module.
504func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
505 return apiScope.apiModuleName(c.moduleBase.BaseModuleName())
506}
507
Paul Duffin56d44902020-01-31 13:36:25 +0000508func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
509 if c.scopePaths == nil {
510 c.scopePaths = make(map[*apiScope]*scopePaths)
511 }
512 paths := c.scopePaths[scope]
513 if paths == nil {
514 paths = &scopePaths{}
515 c.scopePaths[scope] = paths
516 }
517
518 return paths
519}
520
Inseob Kimc0907f12019-02-08 21:00:45 +0900521type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900522 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900523
Sundong Ahn054b19a2018-10-19 13:46:09 +0900524 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900525
Paul Duffin3375e352020-04-28 10:44:03 +0100526 // Map from api scope to the scope specific property structure.
527 scopeToProperties map[*apiScope]*ApiScopeProperties
528
Paul Duffin56d44902020-01-31 13:36:25 +0000529 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900530}
531
Inseob Kimc0907f12019-02-08 21:00:45 +0900532var _ Dependency = (*SdkLibrary)(nil)
533var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800534
Paul Duffin3375e352020-04-28 10:44:03 +0100535func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
536 return module.sdkLibraryProperties.Generate_system_and_test_apis
537}
538
539func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
540 // Check to see if any scopes have been explicitly enabled. If any have then all
541 // must be.
542 anyScopesExplicitlyEnabled := false
543 for _, scope := range allApiScopes {
544 scopeProperties := module.scopeToProperties[scope]
545 if scopeProperties.Enabled != nil {
546 anyScopesExplicitlyEnabled = true
547 break
548 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000549 }
Paul Duffin3375e352020-04-28 10:44:03 +0100550
551 var generatedScopes apiScopes
552 enabledScopes := make(map[*apiScope]struct{})
553 for _, scope := range allApiScopes {
554 scopeProperties := module.scopeToProperties[scope]
555 // If any scopes are explicitly enabled then ignore the legacy enabled status.
556 // This is to ensure that any new usages of this module type do not rely on legacy
557 // behaviour.
558 defaultEnabledStatus := false
559 if anyScopesExplicitlyEnabled {
560 defaultEnabledStatus = scope.defaultEnabledStatus
561 } else {
562 defaultEnabledStatus = scope.legacyEnabledStatus(module)
563 }
564 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
565 if enabled {
566 enabledScopes[scope] = struct{}{}
567 generatedScopes = append(generatedScopes, scope)
568 }
569 }
570
571 // Now check to make sure that any scope that is extended by an enabled scope is also
572 // enabled.
573 for _, scope := range allApiScopes {
574 if _, ok := enabledScopes[scope]; ok {
575 extends := scope.extends
576 if extends != nil {
577 if _, ok := enabledScopes[extends]; !ok {
578 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
579 }
580 }
581 }
582 }
583
584 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000585}
586
Paul Duffine74ac732020-02-06 13:51:46 +0000587var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
588
Jiyong Parke3833882020-02-17 17:28:10 +0900589func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
590 if dt, ok := depTag.(dependencyTag); ok {
591 return dt == xmlPermissionsFileTag
592 }
593 return false
594}
595
Inseob Kimc0907f12019-02-08 21:00:45 +0900596func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100597 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000598 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100599 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000600
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100601 // If the stubs source and API cannot be generated together then add an additional dependency on
602 // the API module.
603 if apiScope.createStubsSourceAndApiTogether {
604 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinc3091c82020-05-08 14:16:20 +0100605 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100606 } else {
607 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinc3091c82020-05-08 14:16:20 +0100608 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
609 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100610 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900611 }
612
Paul Duffine74ac732020-02-06 13:51:46 +0000613 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
614 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900615 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000616 }
617
Sundong Ahn054b19a2018-10-19 13:46:09 +0900618 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900619}
620
Inseob Kimc0907f12019-02-08 21:00:45 +0900621func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000622 // Don't build an implementation library if this is api only.
623 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
624 module.Library.GenerateAndroidBuildActions(ctx)
625 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900626
Sundong Ahn57368eb2018-07-06 11:20:23 +0900627 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000628 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900629 // the recorded paths will be returned depending on the link type of the caller.
630 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900631 tag := ctx.OtherModuleDependencyTag(to)
632
Paul Duffinc8782502020-04-29 20:45:27 +0100633 // Extract information from any of the scope specific dependencies.
634 if scopeTag, ok := tag.(scopeDependencyTag); ok {
635 apiScope := scopeTag.apiScope
636 scopePaths := module.getScopePaths(apiScope)
637
638 // Extract information from the dependency. The exact information extracted
639 // is determined by the nature of the dependency which is determined by the tag.
640 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900641 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900642 })
643}
644
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900645func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000646 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
647 return nil
648 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900649 entriesList := module.Library.AndroidMkEntries()
650 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700651 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900652 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900653}
654
Jiyong Parkc678ad32018-04-10 13:07:10 +0900655// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900656func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900657 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900658}
659
Jiyong Parkc678ad32018-04-10 13:07:10 +0900660// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900661func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900662 return module.BaseModuleName() + sdkXmlFileSuffix
663}
664
Anton Hansson5fd5d242020-03-27 19:43:19 +0000665// The dist path of the stub artifacts
666func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
667 if module.ModuleBase.Owner() != "" {
668 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
669 } else if Bool(module.sdkLibraryProperties.Core_lib) {
670 return path.Join("apistubs", "core", apiScope.name)
671 } else {
672 return path.Join("apistubs", "android", apiScope.name)
673 }
674}
675
Paul Duffin12ceb462019-12-24 20:31:31 +0000676// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +0100677func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000678 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
679 if sdkDep.hasStandardLibs() {
680 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000681 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000682 } else {
683 // Otherwise, use no system module.
684 return "none"
685 }
686}
687
Paul Duffind1b3a922020-01-22 11:57:20 +0000688func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
689 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900690}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900691
Paul Duffind1b3a922020-01-22 11:57:20 +0000692func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
693 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900694}
695
696// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100697func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900698 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900699 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100700 Visibility []string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900701 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000702 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900703 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000704 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000705 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900706 Libs []string
707 Soc_specific *bool
708 Device_specific *bool
709 Product_specific *bool
710 System_ext_specific *bool
711 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900712 Java_version *string
713 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900714 Pdk struct {
715 Enabled *bool
716 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900717 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900718 Openjdk9 struct {
719 Srcs []string
720 Javacflags []string
721 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000722 Dist struct {
723 Targets []string
724 Dest *string
725 Dir *string
726 Tag *string
727 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900728 }{}
729
Paul Duffinc3091c82020-05-08 14:16:20 +0100730 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100731
732 // If stubs_library_visibility is not set then the created module will use the
733 // visibility of this module.
734 visibility := module.sdkLibraryProperties.Stubs_library_visibility
735 props.Visibility = visibility
736
Jiyong Parkc678ad32018-04-10 13:07:10 +0900737 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +0100738 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000739 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100740 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000741 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000742 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000743 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900744 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900745 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900746 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
747 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
748 props.Java_version = module.Library.Module.properties.Java_version
749 if module.Library.Module.deviceProperties.Compile_dex != nil {
750 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900751 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900752
753 if module.SocSpecific() {
754 props.Soc_specific = proptools.BoolPtr(true)
755 } else if module.DeviceSpecific() {
756 props.Device_specific = proptools.BoolPtr(true)
757 } else if module.ProductSpecific() {
758 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900759 } else if module.SystemExtSpecific() {
760 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900761 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000762 // Dist the class jar artifact for sdk builds.
763 if !Bool(module.sdkLibraryProperties.No_dist) {
764 props.Dist.Targets = []string{"sdk", "win_sdk"}
765 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
766 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
767 props.Dist.Tag = proptools.StringPtr(".jar")
768 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900769
Colin Cross84dfc3d2019-09-25 11:33:01 -0700770 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900771}
772
Paul Duffin6d0886e2020-04-07 18:49:53 +0100773// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +0100774// files and also updates and checks the API specification files.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100775func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900776 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900777 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100778 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900779 Srcs []string
780 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100781 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000782 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900783 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000784 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900785 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900786 Java_version *string
787 Merge_annotations_dirs []string
788 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100789 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +0900790 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900791 Current ApiToCheck
792 Last_released ApiToCheck
793 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100794
795 Api_lint struct {
796 Enabled *bool
797 New_since *string
798 Baseline_file *string
799 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900800 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900801 Aidl struct {
802 Include_dirs []string
803 Local_include_dirs []string
804 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000805 Dist struct {
806 Targets []string
807 Dest *string
808 Dir *string
809 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900810 }{}
811
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100812 // The stubs source processing uses the same compile time classpath when extracting the
813 // API from the implementation library as it does when compiling it. i.e. the same
814 // * sdk version
815 // * system_modules
816 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100817
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100818 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +0100819
820 // If stubs_source_visibility is not set then the created module will use the
821 // visibility of this module.
822 visibility := module.sdkLibraryProperties.Stubs_source_visibility
823 props.Visibility = visibility
824
Sundong Ahn054b19a2018-10-19 13:46:09 +0900825 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100826 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000827 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900828 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900829 // A droiddoc module has only one Libs property and doesn't distinguish between
830 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900831 props.Libs = module.Library.Module.properties.Libs
832 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
833 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
834 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900835 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900836
Sundong Ahn054b19a2018-10-19 13:46:09 +0900837 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
838 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
839
Paul Duffin6d0886e2020-04-07 18:49:53 +0100840 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000841 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100842 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000843 }
844 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100845 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000846 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
847 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100848 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000849 disabledWarnings := []string{
850 "MissingPermission",
851 "BroadcastBehavior",
852 "HiddenSuperclass",
853 "DeprecationMismatch",
854 "UnavailableSymbol",
855 "SdkConstant",
856 "HiddenTypeParameter",
857 "Todo",
858 "Typo",
859 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100860 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900861
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100862 if !createStubSources {
863 // Stubs are not required.
864 props.Generate_stubs = proptools.BoolPtr(false)
865 }
866
Paul Duffin1fb487d2020-04-07 18:50:10 +0100867 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100868 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000869 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100870 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900871
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100872 if createApi {
873 // List of APIs identified from the provided source files are created. They are later
874 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
875 // last-released (a.k.a numbered) list of API.
876 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
877 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
878 apiDir := module.getApiDir()
879 currentApiFileName = path.Join(apiDir, currentApiFileName)
880 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900881
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100882 // check against the not-yet-release API
883 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
884 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900885
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100886 if !apiScope.unstable {
887 // check against the latest released API
888 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
889 props.Check_api.Last_released.Api_file = latestApiFilegroupName
890 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
891 module.latestRemovedApiFilegroupName(apiScope))
892 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +0100893
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100894 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
895 // Enable api lint.
896 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
897 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +0100898
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100899 // If it exists then pass a lint-baseline.txt through to droidstubs.
900 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
901 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
902 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
903 if err != nil {
904 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
905 }
906 if len(paths) == 1 {
907 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
908 } else if len(paths) != 0 {
909 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
910 }
Paul Duffin160fe412020-05-10 19:32:20 +0100911 }
912 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900913
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100914 // Dist the api txt artifact for sdk builds.
915 if !Bool(module.sdkLibraryProperties.No_dist) {
916 props.Dist.Targets = []string{"sdk", "win_sdk"}
917 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
918 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
919 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000920 }
921
Colin Cross84dfc3d2019-09-25 11:33:01 -0700922 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900923}
924
Jooyung Han5e9013b2020-03-10 06:23:13 +0900925func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
926 depTag := mctx.OtherModuleDependencyTag(dep)
927 if depTag == xmlPermissionsFileTag {
928 return true
929 }
930 return module.Library.DepIsInSameApex(mctx, dep)
931}
932
Jiyong Parkc678ad32018-04-10 13:07:10 +0900933// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +0100934func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900935 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900936 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900937 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900938 Soc_specific *bool
939 Device_specific *bool
940 Product_specific *bool
941 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900942 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900943 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900944 Name: proptools.StringPtr(module.xmlFileName()),
945 Lib_name: proptools.StringPtr(module.BaseModuleName()),
946 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900947 }
Jiyong Parke3833882020-02-17 17:28:10 +0900948
949 if module.SocSpecific() {
950 props.Soc_specific = proptools.BoolPtr(true)
951 } else if module.DeviceSpecific() {
952 props.Device_specific = proptools.BoolPtr(true)
953 } else if module.ProductSpecific() {
954 props.Product_specific = proptools.BoolPtr(true)
955 } else if module.SystemExtSpecific() {
956 props.System_ext_specific = proptools.BoolPtr(true)
957 }
958
959 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900960}
961
Paul Duffin50061512020-01-21 16:31:05 +0000962func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900963 var ver sdkVersion
964 var kind sdkKind
965 if s.usePrebuilt(ctx) {
966 ver = s.version
967 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900968 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900969 // We don't have prebuilt SDK for the specific sdkVersion.
970 // Instead of breaking the build, fallback to use "system_current"
971 ver = sdkVersionCurrent
972 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900973 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900974
975 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000976 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900977 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900978 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800979 if ctx.Config().AllowMissingDependencies() {
980 return android.Paths{android.PathForSource(ctx, jar)}
981 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900982 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800983 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900984 return nil
985 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900986 return android.Paths{jarPath.Path()}
987}
988
Paul Duffind1b3a922020-01-22 11:57:20 +0000989func (module *SdkLibrary) sdkJars(
990 ctx android.BaseModuleContext,
991 sdkVersion sdkSpec,
992 headerJars bool) android.Paths {
993
Paul Duffin50061512020-01-21 16:31:05 +0000994 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
995 if sdkVersion.version.isNumbered() {
996 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900997 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000998 if !sdkVersion.specified() {
999 if headerJars {
1000 return module.Library.HeaderJars()
1001 } else {
1002 return module.Library.ImplementationJars()
1003 }
1004 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001005 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +09001006 switch sdkVersion.kind {
1007 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +00001008 apiScope = apiScopeSystem
1009 case sdkTest:
1010 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +09001011 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +09001012 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +09001013 default:
Paul Duffin726d23c2020-01-22 16:30:37 +00001014 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +00001015 }
1016
Paul Duffin726d23c2020-01-22 16:30:37 +00001017 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +00001018 if headerJars {
1019 return paths.stubsHeaderPath
1020 } else {
1021 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +09001022 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001023 }
1024}
1025
Sundong Ahn241cd372018-07-13 16:16:44 +09001026// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001027func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1028 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1029}
1030
1031// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001032func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001033 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001034}
1035
Sundong Ahn80a87b32019-05-13 15:02:50 +09001036func (module *SdkLibrary) SetNoDist() {
1037 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1038}
1039
Colin Cross571cccf2019-02-04 11:22:08 -08001040var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1041
Jiyong Park82484c02018-04-23 21:41:26 +09001042func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001043 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001044 return &[]string{}
1045 }).(*[]string)
1046}
1047
Paul Duffin749f98f2019-12-30 17:23:46 +00001048func (module *SdkLibrary) getApiDir() string {
1049 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1050}
1051
Jiyong Parkc678ad32018-04-10 13:07:10 +09001052// For a java_sdk_library module, create internal modules for stubs, docs,
1053// runtime libs and xml file. If requested, the stubs and docs are created twice
1054// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001055func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1056 // If the module has been disabled then don't create any child modules.
1057 if !module.Enabled() {
1058 return
1059 }
1060
Inseob Kim6e93ac92019-03-21 17:43:49 +09001061 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001062 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001063 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001064 }
1065
Paul Duffin37e0b772019-12-30 17:20:10 +00001066 // If this builds against standard libraries (i.e. is not part of the core libraries)
1067 // then assume it provides both system and test apis. Otherwise, assume it does not and
1068 // also assume it does not contribute to the dist build.
1069 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1070 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001071 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001072 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1073
Inseob Kim8098faa2019-03-18 10:19:51 +09001074 missing_current_api := false
1075
Paul Duffin3375e352020-04-28 10:44:03 +01001076 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001077
Paul Duffin749f98f2019-12-30 17:23:46 +00001078 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001079 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001080 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001081 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001082 p := android.ExistentPathForSource(mctx, path)
1083 if !p.Valid() {
1084 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1085 missing_current_api = true
1086 }
1087 }
1088 }
1089
1090 if missing_current_api {
1091 script := "build/soong/scripts/gen-java-current-api-files.sh"
1092 p := android.ExistentPathForSource(mctx, script)
1093
1094 if !p.Valid() {
1095 panic(fmt.Sprintf("script file %s doesn't exist", script))
1096 }
1097
1098 mctx.ModuleErrorf("One or more current api files are missing. "+
1099 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001100 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001101 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001102 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001103 return
1104 }
1105
Paul Duffin3375e352020-04-28 10:44:03 +01001106 for _, scope := range generatedScopes {
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001107 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinc3091c82020-05-08 14:16:20 +01001108 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001109
1110 // If the args needed to generate the stubs and API are the same then they
1111 // can be generated in a single invocation of metalava, otherwise they will
1112 // need separate invocations.
1113 if scope.createStubsSourceAndApiTogether {
1114 // Use the stubs source name for legacy reasons.
1115 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1116 } else {
1117 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1118
1119 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinc3091c82020-05-08 14:16:20 +01001120 apiName := module.apiModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001121 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1122 }
1123
Paul Duffind1b3a922020-01-22 11:57:20 +00001124 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001125 }
1126
Paul Duffin43db9be2019-12-30 17:35:49 +00001127 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
1128 // for runtime
1129 module.createXmlFile(mctx)
1130
1131 // record java_sdk_library modules so that they are exported to make
1132 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1133 javaSdkLibrariesLock.Lock()
1134 defer javaSdkLibrariesLock.Unlock()
1135 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1136 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001137}
1138
1139func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001140 module.AddProperties(
1141 &module.sdkLibraryProperties,
1142 &module.Library.Module.properties,
1143 &module.Library.Module.dexpreoptProperties,
1144 &module.Library.Module.deviceProperties,
1145 &module.Library.Module.protoProperties,
1146 )
1147
1148 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
1149 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001150}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001151
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001152// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1153// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1154// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1155// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1156// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001157func SdkLibraryFactory() android.Module {
1158 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001159
1160 // Initialize information common between source and prebuilt.
1161 module.initCommon(&module.ModuleBase)
1162
Inseob Kimc0907f12019-02-08 21:00:45 +09001163 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001164 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001165 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001166
1167 // Initialize the map from scope to scope specific properties.
1168 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1169 for _, scope := range allApiScopes {
1170 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1171 }
1172 module.scopeToProperties = scopeToProperties
1173
Paul Duffin4911a892020-04-29 23:35:13 +01001174 // Add the properties containing visibility rules so that they are checked.
1175 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1176 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1177
Paul Duffinf0229202020-04-29 16:47:28 +01001178 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001179 return module
1180}
Colin Cross79c7c262019-04-17 11:11:46 -07001181
1182//
1183// SDK library prebuilts
1184//
1185
Paul Duffin56d44902020-01-31 13:36:25 +00001186// Properties associated with each api scope.
1187type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001188 Jars []string `android:"path"`
1189
1190 Sdk_version *string
1191
Colin Cross79c7c262019-04-17 11:11:46 -07001192 // List of shared java libs that this module has dependencies to
1193 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001194
Paul Duffinc8782502020-04-29 20:45:27 +01001195 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001196 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001197
1198 // The current.txt
1199 Current_api string `android:"path"`
1200
1201 // The removed.txt
1202 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001203}
1204
Paul Duffin56d44902020-01-31 13:36:25 +00001205type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001206 // List of shared java libs, common to all scopes, that this module has
1207 // dependencies to
1208 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001209}
1210
Colin Cross79c7c262019-04-17 11:11:46 -07001211type sdkLibraryImport struct {
1212 android.ModuleBase
1213 android.DefaultableModuleBase
1214 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001215 android.ApexModuleBase
1216 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001217
1218 properties sdkLibraryImportProperties
1219
Paul Duffin46a26a82020-04-07 19:27:04 +01001220 // Map from api scope to the scope specific property structure.
1221 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1222
Paul Duffin56d44902020-01-31 13:36:25 +00001223 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001224}
1225
1226var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1227
Paul Duffin46a26a82020-04-07 19:27:04 +01001228// The type of a structure that contains a field of type sdkLibraryScopeProperties
1229// for each apiscope in allApiScopes, e.g. something like:
1230// struct {
1231// Public sdkLibraryScopeProperties
1232// System sdkLibraryScopeProperties
1233// ...
1234// }
1235var allScopeStructType = createAllScopePropertiesStructType()
1236
1237// Dynamically create a structure type for each apiscope in allApiScopes.
1238func createAllScopePropertiesStructType() reflect.Type {
1239 var fields []reflect.StructField
1240 for _, apiScope := range allApiScopes {
1241 field := reflect.StructField{
1242 Name: apiScope.fieldName,
1243 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1244 }
1245 fields = append(fields, field)
1246 }
1247
1248 return reflect.StructOf(fields)
1249}
1250
1251// Create an instance of the scope specific structure type and return a map
1252// from apiscope to a pointer to each scope specific field.
1253func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1254 allScopePropertiesPtr := reflect.New(allScopeStructType)
1255 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1256 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1257
1258 for _, apiScope := range allApiScopes {
1259 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1260 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1261 }
1262
1263 return allScopePropertiesPtr.Interface(), scopeProperties
1264}
1265
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001266// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001267func sdkLibraryImportFactory() android.Module {
1268 module := &sdkLibraryImport{}
1269
Paul Duffin46a26a82020-04-07 19:27:04 +01001270 allScopeProperties, scopeToProperties := createPropertiesInstance()
1271 module.scopeProperties = scopeToProperties
1272 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001273
Paul Duffinc3091c82020-05-08 14:16:20 +01001274 // Initialize information common between source and prebuilt.
1275 module.initCommon(&module.ModuleBase)
1276
Paul Duffin0bdcb272020-02-06 15:24:57 +00001277 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001278 android.InitApexModule(module)
1279 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001280 InitJavaModule(module, android.HostAndDeviceSupported)
1281
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001282 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) { module.createInternalModules(mctx) })
Colin Cross79c7c262019-04-17 11:11:46 -07001283 return module
1284}
1285
1286func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1287 return &module.prebuilt
1288}
1289
1290func (module *sdkLibraryImport) Name() string {
1291 return module.prebuilt.Name(module.ModuleBase.Name())
1292}
1293
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001294func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001295
Paul Duffin50061512020-01-21 16:31:05 +00001296 // If the build is configured to use prebuilts then force this to be preferred.
1297 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1298 module.prebuilt.ForcePrefer()
1299 }
1300
Paul Duffin46a26a82020-04-07 19:27:04 +01001301 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001302 if len(scopeProperties.Jars) == 0 {
1303 continue
1304 }
1305
Paul Duffinbbb546b2020-04-09 00:07:11 +01001306 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001307
1308 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +00001309 }
Colin Cross79c7c262019-04-17 11:11:46 -07001310
1311 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1312 javaSdkLibrariesLock.Lock()
1313 defer javaSdkLibrariesLock.Unlock()
1314 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1315}
1316
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001317func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001318 // Creates a java import for the jar with ".stubs" suffix
1319 props := struct {
1320 Name *string
1321 Soc_specific *bool
1322 Device_specific *bool
1323 Product_specific *bool
1324 System_ext_specific *bool
1325 Sdk_version *string
1326 Libs []string
1327 Jars []string
1328 Prefer *bool
1329 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001330 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001331 props.Sdk_version = scopeProperties.Sdk_version
1332 // Prepend any of the libs from the legacy public properties to the libs for each of the
1333 // scopes to avoid having to duplicate them in each scope.
1334 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1335 props.Jars = scopeProperties.Jars
1336 if module.SocSpecific() {
1337 props.Soc_specific = proptools.BoolPtr(true)
1338 } else if module.DeviceSpecific() {
1339 props.Device_specific = proptools.BoolPtr(true)
1340 } else if module.ProductSpecific() {
1341 props.Product_specific = proptools.BoolPtr(true)
1342 } else if module.SystemExtSpecific() {
1343 props.System_ext_specific = proptools.BoolPtr(true)
1344 }
1345 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
1346 // That will cause the prebuilt version of the stubs to override the source version.
1347 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1348 props.Prefer = proptools.BoolPtr(true)
1349 }
1350 mctx.CreateModule(ImportFactory, &props)
1351}
1352
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001353func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001354 props := struct {
1355 Name *string
1356 Srcs []string
1357 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001358 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001359 props.Srcs = scopeProperties.Stub_srcs
1360 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
1361}
1362
Colin Cross79c7c262019-04-17 11:11:46 -07001363func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001364 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001365 if len(scopeProperties.Jars) == 0 {
1366 continue
1367 }
1368
1369 // Add dependencies to the prebuilt stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001370 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin56d44902020-01-31 13:36:25 +00001371 }
Colin Cross79c7c262019-04-17 11:11:46 -07001372}
1373
1374func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1375 // Record the paths to the prebuilt stubs library.
1376 ctx.VisitDirectDeps(func(to android.Module) {
1377 tag := ctx.OtherModuleDependencyTag(to)
1378
Paul Duffin56d44902020-01-31 13:36:25 +00001379 if lib, ok := to.(Dependency); ok {
1380 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1381 apiScope := scopeTag.apiScope
1382 scopePaths := module.getScopePaths(apiScope)
1383 scopePaths.stubsHeaderPath = lib.HeaderJars()
1384 }
Colin Cross79c7c262019-04-17 11:11:46 -07001385 }
1386 })
1387}
1388
Paul Duffin56d44902020-01-31 13:36:25 +00001389func (module *sdkLibraryImport) sdkJars(
1390 ctx android.BaseModuleContext,
1391 sdkVersion sdkSpec) android.Paths {
1392
Paul Duffin50061512020-01-21 16:31:05 +00001393 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1394 if sdkVersion.version.isNumbered() {
1395 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1396 }
1397
Paul Duffin56d44902020-01-31 13:36:25 +00001398 var apiScope *apiScope
1399 switch sdkVersion.kind {
1400 case sdkSystem:
1401 apiScope = apiScopeSystem
1402 case sdkTest:
1403 apiScope = apiScopeTest
1404 default:
1405 apiScope = apiScopePublic
1406 }
1407
1408 paths := module.getScopePaths(apiScope)
1409 return paths.stubsHeaderPath
1410}
1411
Colin Cross79c7c262019-04-17 11:11:46 -07001412// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001413func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001414 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001415 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001416}
1417
1418// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001419func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001420 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001421 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001422}
Jiyong Parke3833882020-02-17 17:28:10 +09001423
1424//
1425// java_sdk_library_xml
1426//
1427type sdkLibraryXml struct {
1428 android.ModuleBase
1429 android.DefaultableModuleBase
1430 android.ApexModuleBase
1431
1432 properties sdkLibraryXmlProperties
1433
1434 outputFilePath android.OutputPath
1435 installDirPath android.InstallPath
1436}
1437
1438type sdkLibraryXmlProperties struct {
1439 // canonical name of the lib
1440 Lib_name *string
1441}
1442
1443// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1444// Not to be used directly by users. java_sdk_library internally uses this.
1445func sdkLibraryXmlFactory() android.Module {
1446 module := &sdkLibraryXml{}
1447
1448 module.AddProperties(&module.properties)
1449
1450 android.InitApexModule(module)
1451 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1452
1453 return module
1454}
1455
1456// from android.PrebuiltEtcModule
1457func (module *sdkLibraryXml) SubDir() string {
1458 return "permissions"
1459}
1460
1461// from android.PrebuiltEtcModule
1462func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1463 return module.outputFilePath
1464}
1465
1466// from android.ApexModule
1467func (module *sdkLibraryXml) AvailableFor(what string) bool {
1468 return true
1469}
1470
1471func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1472 // do nothing
1473}
1474
1475// File path to the runtime implementation library
1476func (module *sdkLibraryXml) implPath() string {
1477 implName := proptools.String(module.properties.Lib_name)
1478 if apexName := module.ApexName(); apexName != "" {
1479 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1480 // In most cases, this works fine. But when apex_name is set or override_apex is used
1481 // this can be wrong.
1482 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1483 }
1484 partition := "system"
1485 if module.SocSpecific() {
1486 partition = "vendor"
1487 } else if module.DeviceSpecific() {
1488 partition = "odm"
1489 } else if module.ProductSpecific() {
1490 partition = "product"
1491 } else if module.SystemExtSpecific() {
1492 partition = "system_ext"
1493 }
1494 return "/" + partition + "/framework/" + implName + ".jar"
1495}
1496
1497func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1498 libName := proptools.String(module.properties.Lib_name)
1499 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1500
1501 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1502 rule := android.NewRuleBuilder()
1503 rule.Command().
1504 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1505 Output(module.outputFilePath)
1506
1507 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1508
1509 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1510}
1511
1512func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1513 if !module.IsForPlatform() {
1514 return []android.AndroidMkEntries{android.AndroidMkEntries{
1515 Disabled: true,
1516 }}
1517 }
1518
1519 return []android.AndroidMkEntries{android.AndroidMkEntries{
1520 Class: "ETC",
1521 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1522 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1523 func(entries *android.AndroidMkEntries) {
1524 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1525 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1526 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1527 },
1528 },
1529 }}
1530}
Paul Duffindd46f712020-02-10 13:37:10 +00001531
1532type sdkLibrarySdkMemberType struct {
1533 android.SdkMemberTypeBase
1534}
1535
1536func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1537 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1538}
1539
1540func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1541 _, ok := module.(*SdkLibrary)
1542 return ok
1543}
1544
1545func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1546 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1547}
1548
1549func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1550 return &sdkLibrarySdkMemberProperties{}
1551}
1552
1553type sdkLibrarySdkMemberProperties struct {
1554 android.SdkMemberPropertiesBase
1555
1556 // Scope to per scope properties.
1557 Scopes map[*apiScope]scopeProperties
1558
1559 // Additional libraries that the exported stubs libraries depend upon.
1560 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001561
1562 // The Java stubs source files.
1563 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001564}
1565
1566type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001567 Jars android.Paths
1568 StubsSrcJar android.Path
1569 CurrentApiFile android.Path
1570 RemovedApiFile android.Path
1571 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001572}
1573
1574func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1575 sdk := variant.(*SdkLibrary)
1576
1577 s.Scopes = make(map[*apiScope]scopeProperties)
1578 for _, apiScope := range allApiScopes {
1579 paths := sdk.getScopePaths(apiScope)
1580 jars := paths.stubsImplPath
1581 if len(jars) > 0 {
1582 properties := scopeProperties{}
1583 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01001584 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001585 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001586 properties.CurrentApiFile = paths.currentApiFilePath
1587 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001588 s.Scopes[apiScope] = properties
1589 }
1590 }
1591
1592 s.Libs = sdk.properties.Libs
1593}
1594
1595func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1596 for _, apiScope := range allApiScopes {
1597 if properties, ok := s.Scopes[apiScope]; ok {
1598 scopeSet := propertySet.AddPropertySet(apiScope.name)
1599
Paul Duffin3d1248c2020-04-09 00:10:17 +01001600 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1601
Paul Duffindd46f712020-02-10 13:37:10 +00001602 var jars []string
1603 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001604 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001605 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1606 jars = append(jars, dest)
1607 }
1608 scopeSet.AddProperty("jars", jars)
1609
Paul Duffin3d1248c2020-04-09 00:10:17 +01001610 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1611 // the source files are also unpacked.
1612 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1613 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1614 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1615
Paul Duffin1fd005d2020-04-09 01:08:11 +01001616 if properties.CurrentApiFile != nil {
1617 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1618 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1619 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1620 }
1621
1622 if properties.RemovedApiFile != nil {
1623 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1624 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1625 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1626 }
1627
Paul Duffindd46f712020-02-10 13:37:10 +00001628 if properties.SdkVersion != "" {
1629 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1630 }
1631 }
1632 }
1633
1634 if len(s.Libs) > 0 {
1635 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1636 }
1637}