blob: 0415e6dd31c9bab5168103e8648f69554c855de7 [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
194func (scope *apiScope) stubsModuleName(baseName string) string {
195 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 {
482 scopePaths map[*apiScope]*scopePaths
483}
484
485func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
486 if c.scopePaths == nil {
487 c.scopePaths = make(map[*apiScope]*scopePaths)
488 }
489 paths := c.scopePaths[scope]
490 if paths == nil {
491 paths = &scopePaths{}
492 c.scopePaths[scope] = paths
493 }
494
495 return paths
496}
497
Inseob Kimc0907f12019-02-08 21:00:45 +0900498type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900499 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900500
Sundong Ahn054b19a2018-10-19 13:46:09 +0900501 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900502
Paul Duffin3375e352020-04-28 10:44:03 +0100503 // Map from api scope to the scope specific property structure.
504 scopeToProperties map[*apiScope]*ApiScopeProperties
505
Paul Duffin56d44902020-01-31 13:36:25 +0000506 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507}
508
Inseob Kimc0907f12019-02-08 21:00:45 +0900509var _ Dependency = (*SdkLibrary)(nil)
510var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800511
Paul Duffin3375e352020-04-28 10:44:03 +0100512func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
513 return module.sdkLibraryProperties.Generate_system_and_test_apis
514}
515
516func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
517 // Check to see if any scopes have been explicitly enabled. If any have then all
518 // must be.
519 anyScopesExplicitlyEnabled := false
520 for _, scope := range allApiScopes {
521 scopeProperties := module.scopeToProperties[scope]
522 if scopeProperties.Enabled != nil {
523 anyScopesExplicitlyEnabled = true
524 break
525 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000526 }
Paul Duffin3375e352020-04-28 10:44:03 +0100527
528 var generatedScopes apiScopes
529 enabledScopes := make(map[*apiScope]struct{})
530 for _, scope := range allApiScopes {
531 scopeProperties := module.scopeToProperties[scope]
532 // If any scopes are explicitly enabled then ignore the legacy enabled status.
533 // This is to ensure that any new usages of this module type do not rely on legacy
534 // behaviour.
535 defaultEnabledStatus := false
536 if anyScopesExplicitlyEnabled {
537 defaultEnabledStatus = scope.defaultEnabledStatus
538 } else {
539 defaultEnabledStatus = scope.legacyEnabledStatus(module)
540 }
541 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
542 if enabled {
543 enabledScopes[scope] = struct{}{}
544 generatedScopes = append(generatedScopes, scope)
545 }
546 }
547
548 // Now check to make sure that any scope that is extended by an enabled scope is also
549 // enabled.
550 for _, scope := range allApiScopes {
551 if _, ok := enabledScopes[scope]; ok {
552 extends := scope.extends
553 if extends != nil {
554 if _, ok := enabledScopes[extends]; !ok {
555 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
556 }
557 }
558 }
559 }
560
561 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000562}
563
Paul Duffine74ac732020-02-06 13:51:46 +0000564var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
565
Jiyong Parke3833882020-02-17 17:28:10 +0900566func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
567 if dt, ok := depTag.(dependencyTag); ok {
568 return dt == xmlPermissionsFileTag
569 }
570 return false
571}
572
Inseob Kimc0907f12019-02-08 21:00:45 +0900573func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100574 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000575 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000576 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000577
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100578 // If the stubs source and API cannot be generated together then add an additional dependency on
579 // the API module.
580 if apiScope.createStubsSourceAndApiTogether {
581 // Add a dependency on the stubs source in order to access both stubs source and api information.
582 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceName(apiScope))
583 } else {
584 // Add separate dependencies on the creators of the stubs source files and the API.
585 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceName(apiScope))
586 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiName(apiScope))
587 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900588 }
589
Paul Duffine74ac732020-02-06 13:51:46 +0000590 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
591 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900592 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000593 }
594
Sundong Ahn054b19a2018-10-19 13:46:09 +0900595 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900596}
597
Inseob Kimc0907f12019-02-08 21:00:45 +0900598func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000599 // Don't build an implementation library if this is api only.
600 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
601 module.Library.GenerateAndroidBuildActions(ctx)
602 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900603
Sundong Ahn57368eb2018-07-06 11:20:23 +0900604 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000605 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900606 // the recorded paths will be returned depending on the link type of the caller.
607 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900608 tag := ctx.OtherModuleDependencyTag(to)
609
Paul Duffinc8782502020-04-29 20:45:27 +0100610 // Extract information from any of the scope specific dependencies.
611 if scopeTag, ok := tag.(scopeDependencyTag); ok {
612 apiScope := scopeTag.apiScope
613 scopePaths := module.getScopePaths(apiScope)
614
615 // Extract information from the dependency. The exact information extracted
616 // is determined by the nature of the dependency which is determined by the tag.
617 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900618 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900619 })
620}
621
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900622func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000623 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
624 return nil
625 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900626 entriesList := module.Library.AndroidMkEntries()
627 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700628 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900629 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900630}
631
Paul Duffinc8782502020-04-29 20:45:27 +0100632// Name of the java_library module that compiles the stubs source.
Paul Duffind1b3a922020-01-22 11:57:20 +0000633func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
634 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900635}
636
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100637// Name of the droidstubs module that generates the stubs source and may also
638// generate/check the API.
Paul Duffinc8782502020-04-29 20:45:27 +0100639func (module *SdkLibrary) stubsSourceName(apiScope *apiScope) string {
640 return apiScope.stubsSourceModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900641}
642
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100643// Name of the droidstubs module that generates/checks the API. Only used if it
644// requires different arts to the stubs source generating module.
645func (module *SdkLibrary) apiName(apiScope *apiScope) string {
646 return apiScope.apiModuleName(module.BaseModuleName())
647}
648
Jiyong Parkc678ad32018-04-10 13:07:10 +0900649// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900650func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900651 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900652}
653
Jiyong Parkc678ad32018-04-10 13:07:10 +0900654// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900655func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900656 return module.BaseModuleName() + sdkXmlFileSuffix
657}
658
Anton Hansson5fd5d242020-03-27 19:43:19 +0000659// The dist path of the stub artifacts
660func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
661 if module.ModuleBase.Owner() != "" {
662 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
663 } else if Bool(module.sdkLibraryProperties.Core_lib) {
664 return path.Join("apistubs", "core", apiScope.name)
665 } else {
666 return path.Join("apistubs", "android", apiScope.name)
667 }
668}
669
Paul Duffin12ceb462019-12-24 20:31:31 +0000670// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +0100671func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000672 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
673 if sdkDep.hasStandardLibs() {
674 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000675 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000676 } else {
677 // Otherwise, use no system module.
678 return "none"
679 }
680}
681
Paul Duffind1b3a922020-01-22 11:57:20 +0000682func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
683 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900684}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900685
Paul Duffind1b3a922020-01-22 11:57:20 +0000686func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
687 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900688}
689
690// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100691func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900692 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900693 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100694 Visibility []string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900695 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000696 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900697 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000698 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000699 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900700 Libs []string
701 Soc_specific *bool
702 Device_specific *bool
703 Product_specific *bool
704 System_ext_specific *bool
705 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900706 Java_version *string
707 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900708 Pdk struct {
709 Enabled *bool
710 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900711 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900712 Openjdk9 struct {
713 Srcs []string
714 Javacflags []string
715 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000716 Dist struct {
717 Targets []string
718 Dest *string
719 Dir *string
720 Tag *string
721 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900722 }{}
723
Jiyong Parkdf130542018-04-27 16:29:21 +0900724 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100725
726 // If stubs_library_visibility is not set then the created module will use the
727 // visibility of this module.
728 visibility := module.sdkLibraryProperties.Stubs_library_visibility
729 props.Visibility = visibility
730
Jiyong Parkc678ad32018-04-10 13:07:10 +0900731 // sources are generated from the droiddoc
Paul Duffinc8782502020-04-29 20:45:27 +0100732 props.Srcs = []string{":" + module.stubsSourceName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000733 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100734 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000735 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000736 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000737 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900738 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900739 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900740 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
741 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
742 props.Java_version = module.Library.Module.properties.Java_version
743 if module.Library.Module.deviceProperties.Compile_dex != nil {
744 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900745 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900746
747 if module.SocSpecific() {
748 props.Soc_specific = proptools.BoolPtr(true)
749 } else if module.DeviceSpecific() {
750 props.Device_specific = proptools.BoolPtr(true)
751 } else if module.ProductSpecific() {
752 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900753 } else if module.SystemExtSpecific() {
754 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900755 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000756 // Dist the class jar artifact for sdk builds.
757 if !Bool(module.sdkLibraryProperties.No_dist) {
758 props.Dist.Targets = []string{"sdk", "win_sdk"}
759 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
760 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
761 props.Dist.Tag = proptools.StringPtr(".jar")
762 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900763
Colin Cross84dfc3d2019-09-25 11:33:01 -0700764 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900765}
766
Paul Duffin6d0886e2020-04-07 18:49:53 +0100767// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +0100768// files and also updates and checks the API specification files.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100769func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900770 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900771 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100772 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900773 Srcs []string
774 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100775 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000776 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900777 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000778 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900779 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900780 Java_version *string
781 Merge_annotations_dirs []string
782 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100783 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +0900784 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900785 Current ApiToCheck
786 Last_released ApiToCheck
787 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100788
789 Api_lint struct {
790 Enabled *bool
791 New_since *string
792 Baseline_file *string
793 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900794 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900795 Aidl struct {
796 Include_dirs []string
797 Local_include_dirs []string
798 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000799 Dist struct {
800 Targets []string
801 Dest *string
802 Dir *string
803 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900804 }{}
805
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100806 // The stubs source processing uses the same compile time classpath when extracting the
807 // API from the implementation library as it does when compiling it. i.e. the same
808 // * sdk version
809 // * system_modules
810 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100811
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100812 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +0100813
814 // If stubs_source_visibility is not set then the created module will use the
815 // visibility of this module.
816 visibility := module.sdkLibraryProperties.Stubs_source_visibility
817 props.Visibility = visibility
818
Sundong Ahn054b19a2018-10-19 13:46:09 +0900819 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100820 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000821 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900822 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900823 // A droiddoc module has only one Libs property and doesn't distinguish between
824 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900825 props.Libs = module.Library.Module.properties.Libs
826 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
827 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
828 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900829 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900830
Sundong Ahn054b19a2018-10-19 13:46:09 +0900831 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
832 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
833
Paul Duffin6d0886e2020-04-07 18:49:53 +0100834 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000835 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100836 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000837 }
838 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100839 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000840 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
841 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100842 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000843 disabledWarnings := []string{
844 "MissingPermission",
845 "BroadcastBehavior",
846 "HiddenSuperclass",
847 "DeprecationMismatch",
848 "UnavailableSymbol",
849 "SdkConstant",
850 "HiddenTypeParameter",
851 "Todo",
852 "Typo",
853 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100854 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900855
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100856 if !createStubSources {
857 // Stubs are not required.
858 props.Generate_stubs = proptools.BoolPtr(false)
859 }
860
Paul Duffin1fb487d2020-04-07 18:50:10 +0100861 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100862 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000863 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100864 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900865
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100866 if createApi {
867 // List of APIs identified from the provided source files are created. They are later
868 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
869 // last-released (a.k.a numbered) list of API.
870 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
871 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
872 apiDir := module.getApiDir()
873 currentApiFileName = path.Join(apiDir, currentApiFileName)
874 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900875
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100876 // check against the not-yet-release API
877 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
878 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900879
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100880 if !apiScope.unstable {
881 // check against the latest released API
882 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
883 props.Check_api.Last_released.Api_file = latestApiFilegroupName
884 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
885 module.latestRemovedApiFilegroupName(apiScope))
886 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +0100887
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100888 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
889 // Enable api lint.
890 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
891 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +0100892
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100893 // If it exists then pass a lint-baseline.txt through to droidstubs.
894 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
895 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
896 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
897 if err != nil {
898 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
899 }
900 if len(paths) == 1 {
901 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
902 } else if len(paths) != 0 {
903 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
904 }
Paul Duffin160fe412020-05-10 19:32:20 +0100905 }
906 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900907
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100908 // Dist the api txt artifact for sdk builds.
909 if !Bool(module.sdkLibraryProperties.No_dist) {
910 props.Dist.Targets = []string{"sdk", "win_sdk"}
911 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
912 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
913 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000914 }
915
Colin Cross84dfc3d2019-09-25 11:33:01 -0700916 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900917}
918
Jooyung Han5e9013b2020-03-10 06:23:13 +0900919func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
920 depTag := mctx.OtherModuleDependencyTag(dep)
921 if depTag == xmlPermissionsFileTag {
922 return true
923 }
924 return module.Library.DepIsInSameApex(mctx, dep)
925}
926
Jiyong Parkc678ad32018-04-10 13:07:10 +0900927// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +0100928func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900929 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900930 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900931 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900932 Soc_specific *bool
933 Device_specific *bool
934 Product_specific *bool
935 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900936 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900937 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900938 Name: proptools.StringPtr(module.xmlFileName()),
939 Lib_name: proptools.StringPtr(module.BaseModuleName()),
940 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900941 }
Jiyong Parke3833882020-02-17 17:28:10 +0900942
943 if module.SocSpecific() {
944 props.Soc_specific = proptools.BoolPtr(true)
945 } else if module.DeviceSpecific() {
946 props.Device_specific = proptools.BoolPtr(true)
947 } else if module.ProductSpecific() {
948 props.Product_specific = proptools.BoolPtr(true)
949 } else if module.SystemExtSpecific() {
950 props.System_ext_specific = proptools.BoolPtr(true)
951 }
952
953 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900954}
955
Paul Duffin50061512020-01-21 16:31:05 +0000956func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900957 var ver sdkVersion
958 var kind sdkKind
959 if s.usePrebuilt(ctx) {
960 ver = s.version
961 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900962 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900963 // We don't have prebuilt SDK for the specific sdkVersion.
964 // Instead of breaking the build, fallback to use "system_current"
965 ver = sdkVersionCurrent
966 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900967 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900968
969 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000970 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900971 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900972 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800973 if ctx.Config().AllowMissingDependencies() {
974 return android.Paths{android.PathForSource(ctx, jar)}
975 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900976 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800977 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900978 return nil
979 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900980 return android.Paths{jarPath.Path()}
981}
982
Paul Duffind1b3a922020-01-22 11:57:20 +0000983func (module *SdkLibrary) sdkJars(
984 ctx android.BaseModuleContext,
985 sdkVersion sdkSpec,
986 headerJars bool) android.Paths {
987
Paul Duffin50061512020-01-21 16:31:05 +0000988 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
989 if sdkVersion.version.isNumbered() {
990 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900991 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000992 if !sdkVersion.specified() {
993 if headerJars {
994 return module.Library.HeaderJars()
995 } else {
996 return module.Library.ImplementationJars()
997 }
998 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000999 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +09001000 switch sdkVersion.kind {
1001 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +00001002 apiScope = apiScopeSystem
1003 case sdkTest:
1004 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +09001005 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +09001006 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +09001007 default:
Paul Duffin726d23c2020-01-22 16:30:37 +00001008 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +00001009 }
1010
Paul Duffin726d23c2020-01-22 16:30:37 +00001011 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +00001012 if headerJars {
1013 return paths.stubsHeaderPath
1014 } else {
1015 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +09001016 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001017 }
1018}
1019
Sundong Ahn241cd372018-07-13 16:16:44 +09001020// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001021func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1022 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1023}
1024
1025// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001026func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001027 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001028}
1029
Sundong Ahn80a87b32019-05-13 15:02:50 +09001030func (module *SdkLibrary) SetNoDist() {
1031 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1032}
1033
Colin Cross571cccf2019-02-04 11:22:08 -08001034var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1035
Jiyong Park82484c02018-04-23 21:41:26 +09001036func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001037 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001038 return &[]string{}
1039 }).(*[]string)
1040}
1041
Paul Duffin749f98f2019-12-30 17:23:46 +00001042func (module *SdkLibrary) getApiDir() string {
1043 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1044}
1045
Jiyong Parkc678ad32018-04-10 13:07:10 +09001046// For a java_sdk_library module, create internal modules for stubs, docs,
1047// runtime libs and xml file. If requested, the stubs and docs are created twice
1048// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001049func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1050 // If the module has been disabled then don't create any child modules.
1051 if !module.Enabled() {
1052 return
1053 }
1054
Inseob Kim6e93ac92019-03-21 17:43:49 +09001055 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001056 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001057 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001058 }
1059
Paul Duffin37e0b772019-12-30 17:20:10 +00001060 // If this builds against standard libraries (i.e. is not part of the core libraries)
1061 // then assume it provides both system and test apis. Otherwise, assume it does not and
1062 // also assume it does not contribute to the dist build.
1063 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1064 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001065 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001066 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1067
Inseob Kim8098faa2019-03-18 10:19:51 +09001068 missing_current_api := false
1069
Paul Duffin3375e352020-04-28 10:44:03 +01001070 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001071
Paul Duffin749f98f2019-12-30 17:23:46 +00001072 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001073 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001074 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001075 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001076 p := android.ExistentPathForSource(mctx, path)
1077 if !p.Valid() {
1078 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1079 missing_current_api = true
1080 }
1081 }
1082 }
1083
1084 if missing_current_api {
1085 script := "build/soong/scripts/gen-java-current-api-files.sh"
1086 p := android.ExistentPathForSource(mctx, script)
1087
1088 if !p.Valid() {
1089 panic(fmt.Sprintf("script file %s doesn't exist", script))
1090 }
1091
1092 mctx.ModuleErrorf("One or more current api files are missing. "+
1093 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001094 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001095 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001096 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001097 return
1098 }
1099
Paul Duffin3375e352020-04-28 10:44:03 +01001100 for _, scope := range generatedScopes {
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001101 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
1102 stubsSourceModuleName := module.stubsSourceName(scope)
1103
1104 // If the args needed to generate the stubs and API are the same then they
1105 // can be generated in a single invocation of metalava, otherwise they will
1106 // need separate invocations.
1107 if scope.createStubsSourceAndApiTogether {
1108 // Use the stubs source name for legacy reasons.
1109 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1110 } else {
1111 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1112
1113 apiArgs := scope.droidstubsArgsForGeneratingApi
1114 apiName := module.apiName(scope)
1115 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1116 }
1117
Paul Duffind1b3a922020-01-22 11:57:20 +00001118 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001119 }
1120
Paul Duffin43db9be2019-12-30 17:35:49 +00001121 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
1122 // for runtime
1123 module.createXmlFile(mctx)
1124
1125 // record java_sdk_library modules so that they are exported to make
1126 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1127 javaSdkLibrariesLock.Lock()
1128 defer javaSdkLibrariesLock.Unlock()
1129 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1130 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001131}
1132
1133func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001134 module.AddProperties(
1135 &module.sdkLibraryProperties,
1136 &module.Library.Module.properties,
1137 &module.Library.Module.dexpreoptProperties,
1138 &module.Library.Module.deviceProperties,
1139 &module.Library.Module.protoProperties,
1140 )
1141
1142 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
1143 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001144}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001145
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001146// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1147// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1148// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1149// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1150// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001151func SdkLibraryFactory() android.Module {
1152 module := &SdkLibrary{}
1153 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001154 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001155 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001156
1157 // Initialize the map from scope to scope specific properties.
1158 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1159 for _, scope := range allApiScopes {
1160 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1161 }
1162 module.scopeToProperties = scopeToProperties
1163
Paul Duffin4911a892020-04-29 23:35:13 +01001164 // Add the properties containing visibility rules so that they are checked.
1165 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1166 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1167
Paul Duffinf0229202020-04-29 16:47:28 +01001168 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001169 return module
1170}
Colin Cross79c7c262019-04-17 11:11:46 -07001171
1172//
1173// SDK library prebuilts
1174//
1175
Paul Duffin56d44902020-01-31 13:36:25 +00001176// Properties associated with each api scope.
1177type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001178 Jars []string `android:"path"`
1179
1180 Sdk_version *string
1181
Colin Cross79c7c262019-04-17 11:11:46 -07001182 // List of shared java libs that this module has dependencies to
1183 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001184
Paul Duffinc8782502020-04-29 20:45:27 +01001185 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001186 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001187
1188 // The current.txt
1189 Current_api string `android:"path"`
1190
1191 // The removed.txt
1192 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001193}
1194
Paul Duffin56d44902020-01-31 13:36:25 +00001195type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001196 // List of shared java libs, common to all scopes, that this module has
1197 // dependencies to
1198 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001199}
1200
Colin Cross79c7c262019-04-17 11:11:46 -07001201type sdkLibraryImport struct {
1202 android.ModuleBase
1203 android.DefaultableModuleBase
1204 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001205 android.ApexModuleBase
1206 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001207
1208 properties sdkLibraryImportProperties
1209
Paul Duffin46a26a82020-04-07 19:27:04 +01001210 // Map from api scope to the scope specific property structure.
1211 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1212
Paul Duffin56d44902020-01-31 13:36:25 +00001213 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001214}
1215
1216var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1217
Paul Duffin46a26a82020-04-07 19:27:04 +01001218// The type of a structure that contains a field of type sdkLibraryScopeProperties
1219// for each apiscope in allApiScopes, e.g. something like:
1220// struct {
1221// Public sdkLibraryScopeProperties
1222// System sdkLibraryScopeProperties
1223// ...
1224// }
1225var allScopeStructType = createAllScopePropertiesStructType()
1226
1227// Dynamically create a structure type for each apiscope in allApiScopes.
1228func createAllScopePropertiesStructType() reflect.Type {
1229 var fields []reflect.StructField
1230 for _, apiScope := range allApiScopes {
1231 field := reflect.StructField{
1232 Name: apiScope.fieldName,
1233 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1234 }
1235 fields = append(fields, field)
1236 }
1237
1238 return reflect.StructOf(fields)
1239}
1240
1241// Create an instance of the scope specific structure type and return a map
1242// from apiscope to a pointer to each scope specific field.
1243func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1244 allScopePropertiesPtr := reflect.New(allScopeStructType)
1245 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1246 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1247
1248 for _, apiScope := range allApiScopes {
1249 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1250 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1251 }
1252
1253 return allScopePropertiesPtr.Interface(), scopeProperties
1254}
1255
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001256// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001257func sdkLibraryImportFactory() android.Module {
1258 module := &sdkLibraryImport{}
1259
Paul Duffin46a26a82020-04-07 19:27:04 +01001260 allScopeProperties, scopeToProperties := createPropertiesInstance()
1261 module.scopeProperties = scopeToProperties
1262 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001263
Paul Duffin0bdcb272020-02-06 15:24:57 +00001264 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001265 android.InitApexModule(module)
1266 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001267 InitJavaModule(module, android.HostAndDeviceSupported)
1268
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001269 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) { module.createInternalModules(mctx) })
Colin Cross79c7c262019-04-17 11:11:46 -07001270 return module
1271}
1272
1273func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1274 return &module.prebuilt
1275}
1276
1277func (module *sdkLibraryImport) Name() string {
1278 return module.prebuilt.Name(module.ModuleBase.Name())
1279}
1280
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001281func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001282
Paul Duffin50061512020-01-21 16:31:05 +00001283 // If the build is configured to use prebuilts then force this to be preferred.
1284 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1285 module.prebuilt.ForcePrefer()
1286 }
1287
Paul Duffin46a26a82020-04-07 19:27:04 +01001288 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001289 if len(scopeProperties.Jars) == 0 {
1290 continue
1291 }
1292
Paul Duffinbbb546b2020-04-09 00:07:11 +01001293 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001294
1295 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +00001296 }
Colin Cross79c7c262019-04-17 11:11:46 -07001297
1298 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1299 javaSdkLibrariesLock.Lock()
1300 defer javaSdkLibrariesLock.Unlock()
1301 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1302}
1303
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001304func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001305 // Creates a java import for the jar with ".stubs" suffix
1306 props := struct {
1307 Name *string
1308 Soc_specific *bool
1309 Device_specific *bool
1310 Product_specific *bool
1311 System_ext_specific *bool
1312 Sdk_version *string
1313 Libs []string
1314 Jars []string
1315 Prefer *bool
1316 }{}
1317 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
1318 props.Sdk_version = scopeProperties.Sdk_version
1319 // Prepend any of the libs from the legacy public properties to the libs for each of the
1320 // scopes to avoid having to duplicate them in each scope.
1321 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1322 props.Jars = scopeProperties.Jars
1323 if module.SocSpecific() {
1324 props.Soc_specific = proptools.BoolPtr(true)
1325 } else if module.DeviceSpecific() {
1326 props.Device_specific = proptools.BoolPtr(true)
1327 } else if module.ProductSpecific() {
1328 props.Product_specific = proptools.BoolPtr(true)
1329 } else if module.SystemExtSpecific() {
1330 props.System_ext_specific = proptools.BoolPtr(true)
1331 }
1332 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
1333 // That will cause the prebuilt version of the stubs to override the source version.
1334 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1335 props.Prefer = proptools.BoolPtr(true)
1336 }
1337 mctx.CreateModule(ImportFactory, &props)
1338}
1339
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001340func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001341 props := struct {
1342 Name *string
1343 Srcs []string
1344 }{}
Paul Duffinc8782502020-04-29 20:45:27 +01001345 props.Name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001346 props.Srcs = scopeProperties.Stub_srcs
1347 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
1348}
1349
Colin Cross79c7c262019-04-17 11:11:46 -07001350func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001351 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001352 if len(scopeProperties.Jars) == 0 {
1353 continue
1354 }
1355
1356 // Add dependencies to the prebuilt stubs library
1357 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
1358 }
Colin Cross79c7c262019-04-17 11:11:46 -07001359}
1360
1361func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1362 // Record the paths to the prebuilt stubs library.
1363 ctx.VisitDirectDeps(func(to android.Module) {
1364 tag := ctx.OtherModuleDependencyTag(to)
1365
Paul Duffin56d44902020-01-31 13:36:25 +00001366 if lib, ok := to.(Dependency); ok {
1367 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1368 apiScope := scopeTag.apiScope
1369 scopePaths := module.getScopePaths(apiScope)
1370 scopePaths.stubsHeaderPath = lib.HeaderJars()
1371 }
Colin Cross79c7c262019-04-17 11:11:46 -07001372 }
1373 })
1374}
1375
Paul Duffin56d44902020-01-31 13:36:25 +00001376func (module *sdkLibraryImport) sdkJars(
1377 ctx android.BaseModuleContext,
1378 sdkVersion sdkSpec) android.Paths {
1379
Paul Duffin50061512020-01-21 16:31:05 +00001380 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1381 if sdkVersion.version.isNumbered() {
1382 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1383 }
1384
Paul Duffin56d44902020-01-31 13:36:25 +00001385 var apiScope *apiScope
1386 switch sdkVersion.kind {
1387 case sdkSystem:
1388 apiScope = apiScopeSystem
1389 case sdkTest:
1390 apiScope = apiScopeTest
1391 default:
1392 apiScope = apiScopePublic
1393 }
1394
1395 paths := module.getScopePaths(apiScope)
1396 return paths.stubsHeaderPath
1397}
1398
Colin Cross79c7c262019-04-17 11:11:46 -07001399// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001400func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001401 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001402 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001403}
1404
1405// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001406func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001407 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001408 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001409}
Jiyong Parke3833882020-02-17 17:28:10 +09001410
1411//
1412// java_sdk_library_xml
1413//
1414type sdkLibraryXml struct {
1415 android.ModuleBase
1416 android.DefaultableModuleBase
1417 android.ApexModuleBase
1418
1419 properties sdkLibraryXmlProperties
1420
1421 outputFilePath android.OutputPath
1422 installDirPath android.InstallPath
1423}
1424
1425type sdkLibraryXmlProperties struct {
1426 // canonical name of the lib
1427 Lib_name *string
1428}
1429
1430// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1431// Not to be used directly by users. java_sdk_library internally uses this.
1432func sdkLibraryXmlFactory() android.Module {
1433 module := &sdkLibraryXml{}
1434
1435 module.AddProperties(&module.properties)
1436
1437 android.InitApexModule(module)
1438 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1439
1440 return module
1441}
1442
1443// from android.PrebuiltEtcModule
1444func (module *sdkLibraryXml) SubDir() string {
1445 return "permissions"
1446}
1447
1448// from android.PrebuiltEtcModule
1449func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1450 return module.outputFilePath
1451}
1452
1453// from android.ApexModule
1454func (module *sdkLibraryXml) AvailableFor(what string) bool {
1455 return true
1456}
1457
1458func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1459 // do nothing
1460}
1461
1462// File path to the runtime implementation library
1463func (module *sdkLibraryXml) implPath() string {
1464 implName := proptools.String(module.properties.Lib_name)
1465 if apexName := module.ApexName(); apexName != "" {
1466 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1467 // In most cases, this works fine. But when apex_name is set or override_apex is used
1468 // this can be wrong.
1469 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1470 }
1471 partition := "system"
1472 if module.SocSpecific() {
1473 partition = "vendor"
1474 } else if module.DeviceSpecific() {
1475 partition = "odm"
1476 } else if module.ProductSpecific() {
1477 partition = "product"
1478 } else if module.SystemExtSpecific() {
1479 partition = "system_ext"
1480 }
1481 return "/" + partition + "/framework/" + implName + ".jar"
1482}
1483
1484func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1485 libName := proptools.String(module.properties.Lib_name)
1486 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1487
1488 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1489 rule := android.NewRuleBuilder()
1490 rule.Command().
1491 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1492 Output(module.outputFilePath)
1493
1494 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1495
1496 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1497}
1498
1499func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1500 if !module.IsForPlatform() {
1501 return []android.AndroidMkEntries{android.AndroidMkEntries{
1502 Disabled: true,
1503 }}
1504 }
1505
1506 return []android.AndroidMkEntries{android.AndroidMkEntries{
1507 Class: "ETC",
1508 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1509 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1510 func(entries *android.AndroidMkEntries) {
1511 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1512 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1513 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1514 },
1515 },
1516 }}
1517}
Paul Duffindd46f712020-02-10 13:37:10 +00001518
1519type sdkLibrarySdkMemberType struct {
1520 android.SdkMemberTypeBase
1521}
1522
1523func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1524 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1525}
1526
1527func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1528 _, ok := module.(*SdkLibrary)
1529 return ok
1530}
1531
1532func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1533 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1534}
1535
1536func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1537 return &sdkLibrarySdkMemberProperties{}
1538}
1539
1540type sdkLibrarySdkMemberProperties struct {
1541 android.SdkMemberPropertiesBase
1542
1543 // Scope to per scope properties.
1544 Scopes map[*apiScope]scopeProperties
1545
1546 // Additional libraries that the exported stubs libraries depend upon.
1547 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001548
1549 // The Java stubs source files.
1550 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001551}
1552
1553type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001554 Jars android.Paths
1555 StubsSrcJar android.Path
1556 CurrentApiFile android.Path
1557 RemovedApiFile android.Path
1558 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001559}
1560
1561func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1562 sdk := variant.(*SdkLibrary)
1563
1564 s.Scopes = make(map[*apiScope]scopeProperties)
1565 for _, apiScope := range allApiScopes {
1566 paths := sdk.getScopePaths(apiScope)
1567 jars := paths.stubsImplPath
1568 if len(jars) > 0 {
1569 properties := scopeProperties{}
1570 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01001571 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001572 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001573 properties.CurrentApiFile = paths.currentApiFilePath
1574 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001575 s.Scopes[apiScope] = properties
1576 }
1577 }
1578
1579 s.Libs = sdk.properties.Libs
1580}
1581
1582func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1583 for _, apiScope := range allApiScopes {
1584 if properties, ok := s.Scopes[apiScope]; ok {
1585 scopeSet := propertySet.AddPropertySet(apiScope.name)
1586
Paul Duffin3d1248c2020-04-09 00:10:17 +01001587 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1588
Paul Duffindd46f712020-02-10 13:37:10 +00001589 var jars []string
1590 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001591 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001592 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1593 jars = append(jars, dest)
1594 }
1595 scopeSet.AddProperty("jars", jars)
1596
Paul Duffin3d1248c2020-04-09 00:10:17 +01001597 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1598 // the source files are also unpacked.
1599 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1600 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1601 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1602
Paul Duffin1fd005d2020-04-09 01:08:11 +01001603 if properties.CurrentApiFile != nil {
1604 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1605 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1606 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1607 }
1608
1609 if properties.RemovedApiFile != nil {
1610 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1611 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1612 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1613 }
1614
Paul Duffindd46f712020-02-10 13:37:10 +00001615 if properties.SdkVersion != "" {
1616 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1617 }
1618 }
1619 }
1620
1621 if len(s.Libs) > 0 {
1622 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1623 }
1624}