blob: b487efdbd8023915c0b253d25fb8e80d12fb6757 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090031)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Paul Duffindd9d0742020-05-08 15:52:37 +010034 sdkXmlFileSuffix = ".xml"
35 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090036 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090038 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 ` you may not use this file except in compliance with the License.\n` +
40 ` You may obtain a copy of the License at\n` +
41 `\n` +
42 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
43 `\n` +
44 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090045 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090046 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47 ` See the License for the specific language governing permissions and\n` +
48 ` limitations under the License.\n` +
49 `-->\n` +
50 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090051 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090052 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090053)
54
Paul Duffind1b3a922020-01-22 11:57:20 +000055// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57 blueprint.BaseDependencyTag
58 name string
59 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010060
61 // Function for extracting appropriate path information from the dependency.
62 depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67 err := tag.depInfoExtractor(paths, dep)
68 if err != nil {
69 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70 }
Paul Duffind1b3a922020-01-22 11:57:20 +000071}
72
73// Provides information about an api scope, e.g. public, system, test.
74type apiScope struct {
75 // The name of the api scope, e.g. public, system, test
76 name string
77
Paul Duffin97b53b82020-05-05 14:40:52 +010078 // The api scope that this scope extends.
79 extends *apiScope
80
Paul Duffin3375e352020-04-28 10:44:03 +010081 // The legacy enabled status for a specific scope can be dependent on other
82 // properties that have been specified on the library so it is provided by
83 // a function that can determine the status by examining those properties.
84 legacyEnabledStatus func(module *SdkLibrary) bool
85
86 // The default enabled status for non-legacy behavior, which is triggered by
87 // explicitly enabling at least one api scope.
88 defaultEnabledStatus bool
89
90 // Gets a pointer to the scope specific properties.
91 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
92
Paul Duffin46a26a82020-04-07 19:27:04 +010093 // The name of the field in the dynamically created structure.
94 fieldName string
95
Paul Duffin6b836ba2020-05-13 19:19:49 +010096 // The name of the property in the java_sdk_library_import
97 propertyName string
98
Paul Duffind1b3a922020-01-22 11:57:20 +000099 // The tag to use to depend on the stubs library module.
100 stubsTag scopeDependencyTag
101
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100102 // The tag to use to depend on the stubs source module (if separate from the API module).
103 stubsSourceTag scopeDependencyTag
104
105 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
106 apiFileTag scopeDependencyTag
107
Paul Duffinc8782502020-04-29 20:45:27 +0100108 // The tag to use to depend on the stubs source and API module.
109 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
111 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
112 apiFilePrefix string
113
114 // The scope specific prefix to add to the sdk library module name to construct a scope specific
115 // module name.
116 moduleSuffix string
117
Paul Duffind1b3a922020-01-22 11:57:20 +0000118 // SDK version that the stubs library is built against. Note that this is always
119 // *current. Older stubs library built with a numbered SDK version is created from
120 // the prebuilt jar.
121 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100122
123 // Extra arguments to pass to droidstubs for this scope.
124 droidstubsArgs []string
Anton Hansson6478ac12020-05-02 11:19:36 +0100125
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100126 // The args that must be passed to droidstubs to generate the stubs source
127 // for this scope.
128 //
129 // The stubs source must include the definitions of everything that is in this
130 // api scope and all the scopes that this one extends.
131 droidstubsArgsForGeneratingStubsSource []string
132
133 // The args that must be passed to droidstubs to generate the API for this scope.
134 //
135 // The API only includes the additional members that this scope adds over the scope
136 // that it extends.
137 droidstubsArgsForGeneratingApi []string
138
139 // True if the stubs source and api can be created by the same metalava invocation.
140 createStubsSourceAndApiTogether bool
141
Anton Hansson6478ac12020-05-02 11:19:36 +0100142 // Whether the api scope can be treated as unstable, and should skip compat checks.
143 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000144}
145
146// Initialize a scope, creating and adding appropriate dependency tags
147func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100148 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100149 scopeByName[name] = scope
150 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100151 scope.propertyName = strings.ReplaceAll(name, "-", "_")
152 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000153 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100154 name: name + "-stubs",
155 apiScope: scope,
156 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000157 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100158 scope.stubsSourceTag = scopeDependencyTag{
159 name: name + "-stubs-source",
160 apiScope: scope,
161 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
162 }
163 scope.apiFileTag = scopeDependencyTag{
164 name: name + "-api",
165 apiScope: scope,
166 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
167 }
Paul Duffinc8782502020-04-29 20:45:27 +0100168 scope.stubsSourceAndApiTag = scopeDependencyTag{
169 name: name + "-stubs-source-and-api",
170 apiScope: scope,
171 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000172 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100173
174 // To get the args needed to generate the stubs source append all the args from
175 // this scope and all the scopes it extends as each set of args adds additional
176 // members to the stubs.
177 var stubsSourceArgs []string
178 for s := scope; s != nil; s = s.extends {
179 stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
180 }
181 scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
182
183 // Currently the args needed to generate the API are the same as the args
184 // needed to add additional members.
185 apiArgs := scope.droidstubsArgs
186 scope.droidstubsArgsForGeneratingApi = apiArgs
187
188 // If the args needed to generate the stubs and API are the same then they
189 // can be generated in a single invocation of metalava, otherwise they will
190 // need separate invocations.
191 scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
192
Paul Duffind1b3a922020-01-22 11:57:20 +0000193 return scope
194}
195
Paul Duffinc3091c82020-05-08 14:16:20 +0100196func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100197 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000198}
199
Paul Duffinc8782502020-04-29 20:45:27 +0100200func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100201 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000202}
203
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100204func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100205 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100206}
207
Paul Duffin3375e352020-04-28 10:44:03 +0100208func (scope *apiScope) String() string {
209 return scope.name
210}
211
Paul Duffind1b3a922020-01-22 11:57:20 +0000212type apiScopes []*apiScope
213
214func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
215 var list []string
216 for _, scope := range scopes {
217 list = append(list, accessor(scope))
218 }
219 return list
220}
221
Jiyong Parkc678ad32018-04-10 13:07:10 +0900222var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100223 scopeByName = make(map[string]*apiScope)
224 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000225 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100226 name: "public",
227
228 // Public scope is enabled by default for both legacy and non-legacy modes.
229 legacyEnabledStatus: func(module *SdkLibrary) bool {
230 return true
231 },
232 defaultEnabledStatus: true,
233
234 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
235 return &module.sdkLibraryProperties.Public
236 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000237 sdkVersion: "current",
238 })
239 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100240 name: "system",
241 extends: apiScopePublic,
242 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
243 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
244 return &module.sdkLibraryProperties.System
245 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100246 apiFilePrefix: "system-",
Paul Duffindd9d0742020-05-08 15:52:37 +0100247 moduleSuffix: ".system",
Anton Hansson6affb1f2020-04-28 16:47:41 +0100248 sdkVersion: "system_current",
Paul Duffin0d543642020-04-29 22:18:41 +0100249 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000250 })
251 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100252 name: "test",
253 extends: apiScopePublic,
254 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
255 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
256 return &module.sdkLibraryProperties.Test
257 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100258 apiFilePrefix: "test-",
Paul Duffindd9d0742020-05-08 15:52:37 +0100259 moduleSuffix: ".test",
Anton Hansson6affb1f2020-04-28 16:47:41 +0100260 sdkVersion: "test_current",
261 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson6478ac12020-05-02 11:19:36 +0100262 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000263 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100264 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100265 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100266 extends: apiScopeSystem,
267 // Module_lib scope is disabled by default in legacy mode.
268 //
269 // Enabling this would break existing usages.
270 legacyEnabledStatus: func(module *SdkLibrary) bool {
271 return false
272 },
273 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
274 return &module.sdkLibraryProperties.Module_lib
275 },
276 apiFilePrefix: "module-lib-",
277 moduleSuffix: ".module_lib",
278 sdkVersion: "module_current",
279 droidstubsArgs: []string{
280 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
281 },
282 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000283 allApiScopes = apiScopes{
284 apiScopePublic,
285 apiScopeSystem,
286 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100287 apiScopeModuleLib,
Paul Duffind1b3a922020-01-22 11:57:20 +0000288 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900289)
290
Jiyong Park82484c02018-04-23 21:41:26 +0900291var (
292 javaSdkLibrariesLock sync.Mutex
293)
294
Jiyong Parkc678ad32018-04-10 13:07:10 +0900295// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900296// 1) disallowing linking to the runtime shared lib
297// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900298
299func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000300 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900301
Jiyong Park82484c02018-04-23 21:41:26 +0900302 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
303 javaSdkLibraries := javaSdkLibraries(ctx.Config())
304 sort.Strings(*javaSdkLibraries)
305 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
306 })
Paul Duffindd46f712020-02-10 13:37:10 +0000307
308 // Register sdk member types.
309 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
310 android.SdkMemberTypeBase{
311 PropertyName: "java_sdk_libs",
312 SupportsSdk: true,
313 },
314 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900315}
316
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000317func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
318 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
319 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
320}
321
Paul Duffin3375e352020-04-28 10:44:03 +0100322// Properties associated with each api scope.
323type ApiScopeProperties struct {
324 // Indicates whether the api surface is generated.
325 //
326 // If this is set for any scope then all scopes must explicitly specify if they
327 // are enabled. This is to prevent new usages from depending on legacy behavior.
328 //
329 // Otherwise, if this is not set for any scope then the default behavior is
330 // scope specific so please refer to the scope specific property documentation.
331 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100332
333 // The sdk_version to use for building the stubs.
334 //
335 // If not specified then it will use an sdk_version determined as follows:
336 // 1) If the sdk_version specified on the java_sdk_library is none then this
337 // will be none. This is used for java_sdk_library instances that are used
338 // to create stubs that contribute to the core_current sdk version.
339 // 2) Otherwise, it is assumed that this library extends but does not contribute
340 // directly to a specific sdk_version and so this uses the sdk_version appropriate
341 // for the api scope. e.g. public will use sdk_version: current, system will use
342 // sdk_version: system_current, etc.
343 //
344 // This does not affect the sdk_version used for either generating the stubs source
345 // or the API file. They both have to use the same sdk_version as is used for
346 // compiling the implementation library.
347 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100348}
349
Jiyong Parkc678ad32018-04-10 13:07:10 +0900350type sdkLibraryProperties struct {
Paul Duffin4911a892020-04-29 23:35:13 +0100351 // Visibility for stubs library modules. If not specified then defaults to the
352 // visibility property.
353 Stubs_library_visibility []string
354
355 // Visibility for stubs source modules. If not specified then defaults to the
356 // visibility property.
357 Stubs_source_visibility []string
358
Sundong Ahnf043cf62018-06-25 16:04:37 +0900359 // List of Java libraries that will be in the classpath when building stubs
360 Stub_only_libs []string `android:"arch_variant"`
361
Paul Duffin7a586d32019-12-30 17:09:34 +0000362 // list of package names that will be documented and publicized as API.
363 // This allows the API to be restricted to a subset of the source files provided.
364 // If this is unspecified then all the source files will be treated as being part
365 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900366 Api_packages []string
367
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900368 // list of package names that must be hidden from the API
369 Hidden_api_packages []string
370
Paul Duffin749f98f2019-12-30 17:23:46 +0000371 // the relative path to the directory containing the api specification files.
372 // Defaults to "api".
373 Api_dir *string
374
Paul Duffindfa131e2020-05-15 20:37:11 +0100375 // Determines whether a runtime implementation library is built; defaults to false.
376 //
377 // If true then it also prevents the module from being used as a shared module, i.e.
378 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000379 Api_only *bool
380
Paul Duffin11512472019-02-11 15:55:17 +0000381 // local files that are used within user customized droiddoc options.
382 Droiddoc_option_files []string
383
384 // additional droiddoc options
385 // Available variables for substitution:
386 //
387 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900388 Droiddoc_options []string
389
Sundong Ahn054b19a2018-10-19 13:46:09 +0900390 // a list of top-level directories containing files to merge qualifier annotations
391 // (i.e. those intended to be included in the stubs written) from.
392 Merge_annotations_dirs []string
393
394 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
395 Merge_inclusion_annotations_dirs []string
396
397 // If set to true, the path of dist files is apistubs/core. Defaults to false.
398 Core_lib *bool
399
Sundong Ahn80a87b32019-05-13 15:02:50 +0900400 // don't create dist rules.
401 No_dist *bool `blueprint:"mutated"`
402
Paul Duffin3375e352020-04-28 10:44:03 +0100403 // indicates whether system and test apis should be generated.
404 Generate_system_and_test_apis bool `blueprint:"mutated"`
405
406 // The properties specific to the public api scope
407 //
408 // Unless explicitly specified by using public.enabled the public api scope is
409 // enabled by default in both legacy and non-legacy mode.
410 Public ApiScopeProperties
411
412 // The properties specific to the system api scope
413 //
414 // In legacy mode the system api scope is enabled by default when sdk_version
415 // is set to something other than "none".
416 //
417 // In non-legacy mode the system api scope is disabled by default.
418 System ApiScopeProperties
419
420 // The properties specific to the test api scope
421 //
422 // In legacy mode the test api scope is enabled by default when sdk_version
423 // is set to something other than "none".
424 //
425 // In non-legacy mode the test api scope is disabled by default.
426 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000427
Paul Duffin8f265b92020-04-28 14:13:56 +0100428 // The properties specific to the module_lib api scope
429 //
430 // Unless explicitly specified by using test.enabled the module_lib api scope is
431 // disabled by default.
432 Module_lib ApiScopeProperties
433
Paul Duffin160fe412020-05-10 19:32:20 +0100434 // Properties related to api linting.
435 Api_lint struct {
436 // Enable api linting.
437 Enabled *bool
438 }
439
Jiyong Parkc678ad32018-04-10 13:07:10 +0900440 // TODO: determines whether to create HTML doc or not
441 //Html_doc *bool
442}
443
Paul Duffin0f8faff2020-05-20 16:18:00 +0100444// Paths to outputs from java_sdk_library and java_sdk_library_import.
445//
446// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
447// OptionalPaths are always set by java_sdk_library but may not be set by
448// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000449type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100450 // The path (represented as Paths for convenience when returning) to the stubs header jar.
451 //
452 // That is the jar that is created by turbine.
453 stubsHeaderPath android.Paths
454
455 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
456 //
457 // This is not the implementation jar, it still only contains stubs.
458 stubsImplPath android.Paths
459
460 // The API specification file, e.g. system_current.txt.
461 currentApiFilePath android.OptionalPath
462
463 // The specification of API elements removed since the last release.
464 removedApiFilePath android.OptionalPath
465
466 // The stubs source jar.
467 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000468}
469
Paul Duffinc8782502020-04-29 20:45:27 +0100470func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
471 if lib, ok := dep.(Dependency); ok {
472 paths.stubsHeaderPath = lib.HeaderJars()
473 paths.stubsImplPath = lib.ImplementationJars()
474 return nil
475 } else {
476 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
477 }
478}
479
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100480func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
481 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
482 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100483 return nil
484 } else {
485 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
486 }
487}
488
Paul Duffin0f8faff2020-05-20 16:18:00 +0100489func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
490 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
491 action(apiStubsProvider)
492 return nil
493 } else {
494 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
495 }
496}
497
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100498func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100499 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
500 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100501}
502
503func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
504 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
505 paths.extractApiInfoFromApiStubsProvider(provider)
506 })
507}
508
Paul Duffin0f8faff2020-05-20 16:18:00 +0100509func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
510 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100511}
512
513func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100514 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100515 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
516 })
517}
518
519func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
520 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
521 paths.extractApiInfoFromApiStubsProvider(provider)
522 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
523 })
524}
525
526type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100527 // The naming scheme to use for the components that this module creates.
528 //
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100529 // If not specified then it defaults to "default". The other allowable value is
530 // "framework-modules" which matches the scheme currently used by framework modules
531 // for the equivalent components represented as separate Soong modules.
Paul Duffin1b1e8062020-05-08 13:44:43 +0100532 //
533 // This is a temporary mechanism to simplify conversion from separate modules for each
534 // component that follow a different naming pattern to the default one.
535 //
536 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100537 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100538
539 // Specifies whether this module can be used as an Android shared library; defaults
540 // to true.
541 //
542 // An Android shared library is one that can be referenced in a <uses-library> element
543 // in an AndroidManifest.xml.
544 Shared_library *bool
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100545}
546
Paul Duffin56d44902020-01-31 13:36:25 +0000547// Common code between sdk library and sdk library import
548type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100549 moduleBase *android.ModuleBase
550
Paul Duffin56d44902020-01-31 13:36:25 +0000551 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100552
553 namingScheme sdkLibraryComponentNamingScheme
554
Paul Duffindfa131e2020-05-15 20:37:11 +0100555 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100556
557 // Functionality related to this being used as a component of a java_sdk_library.
558 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000559}
560
Paul Duffinc3091c82020-05-08 14:16:20 +0100561func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
562 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100563
Paul Duffindfa131e2020-05-15 20:37:11 +0100564 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100565
566 // Initialize this as an sdk library component.
567 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100568}
569
570func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100571 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100572 switch schemeProperty {
573 case "default":
574 c.namingScheme = &defaultNamingScheme{}
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100575 case "framework-modules":
576 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1b1e8062020-05-08 13:44:43 +0100577 default:
578 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
579 return false
580 }
581
Paul Duffindfa131e2020-05-15 20:37:11 +0100582 // Only track this sdk library if this can be used as a shared library.
583 if c.sharedLibrary() {
584 // Use the name specified in the module definition as the owner.
585 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
586 }
Paul Duffin859fe962020-05-15 10:20:31 +0100587
Paul Duffin1b1e8062020-05-08 13:44:43 +0100588 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100589}
590
591// Name of the java_library module that compiles the stubs source.
592func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100593 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100594}
595
596// Name of the droidstubs module that generates the stubs source and may also
597// generate/check the API.
598func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100599 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100600}
601
602// Name of the droidstubs module that generates/checks the API. Only used if it
603// requires different arts to the stubs source generating module.
604func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100605 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100606}
607
Paul Duffin46dc45a2020-05-14 15:39:10 +0100608// The component names for different outputs of the java_sdk_library.
609//
610// They are similar to the names used for the child modules it creates
611const (
612 stubsSourceComponentName = "stubs.source"
613
614 apiTxtComponentName = "api.txt"
615
616 removedApiTxtComponentName = "removed-api.txt"
617)
618
619// A regular expression to match tags that reference a specific stubs component.
620//
621// It will only match if given a valid scope and a valid component. It is verfy strict
622// to ensure it does not accidentally match a similar looking tag that should be processed
623// by the embedded Library.
624var tagSplitter = func() *regexp.Regexp {
625 // Given a list of literal string items returns a regular expression that will
626 // match any one of the items.
627 choice := func(items ...string) string {
628 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
629 }
630
631 // Regular expression to match one of the scopes.
632 scopesRegexp := choice(allScopeNames...)
633
634 // Regular expression to match one of the components.
635 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
636
637 // Regular expression to match any combination of one scope and one component.
638 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
639}()
640
641// For OutputFileProducer interface
642//
643// .<scope>.stubs.source
644// .<scope>.api.txt
645// .<scope>.removed-api.txt
646func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
647 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
648 scopeName := groups[1]
649 component := groups[2]
650
651 if scope, ok := scopeByName[scopeName]; ok {
652 paths := c.findScopePaths(scope)
653 if paths == nil {
654 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
655 }
656
657 switch component {
658 case stubsSourceComponentName:
659 if paths.stubsSrcJar.Valid() {
660 return android.Paths{paths.stubsSrcJar.Path()}, nil
661 }
662
663 case apiTxtComponentName:
664 if paths.currentApiFilePath.Valid() {
665 return android.Paths{paths.currentApiFilePath.Path()}, nil
666 }
667
668 case removedApiTxtComponentName:
669 if paths.removedApiFilePath.Valid() {
670 return android.Paths{paths.removedApiFilePath.Path()}, nil
671 }
672 }
673
674 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
675 } else {
676 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
677 }
678
679 } else {
680 return nil, nil
681 }
682}
683
Paul Duffin803a9562020-05-20 11:52:25 +0100684func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000685 if c.scopePaths == nil {
686 c.scopePaths = make(map[*apiScope]*scopePaths)
687 }
688 paths := c.scopePaths[scope]
689 if paths == nil {
690 paths = &scopePaths{}
691 c.scopePaths[scope] = paths
692 }
693
694 return paths
695}
696
Paul Duffin803a9562020-05-20 11:52:25 +0100697func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
698 if c.scopePaths == nil {
699 return nil
700 }
701
702 return c.scopePaths[scope]
703}
704
705// If this does not support the requested api scope then find the closest available
706// scope it does support. Returns nil if no such scope is available.
707func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
708 for s := scope; s != nil; s = s.extends {
709 if paths := c.findScopePaths(s); paths != nil {
710 return paths
711 }
712 }
713
714 // This should never happen outside tests as public should be the base scope for every
715 // scope and is enabled by default.
716 return nil
717}
718
Paul Duffin23970f42020-05-20 14:20:02 +0100719func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100720
721 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
722 if sdkVersion.version.isNumbered() {
723 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
724 }
725
726 var apiScope *apiScope
727 switch sdkVersion.kind {
728 case sdkSystem:
729 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100730 case sdkModule:
731 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100732 case sdkTest:
733 apiScope = apiScopeTest
734 default:
735 apiScope = apiScopePublic
736 }
737
Paul Duffin803a9562020-05-20 11:52:25 +0100738 paths := c.findClosestScopePath(apiScope)
739 if paths == nil {
740 var scopes []string
741 for _, s := range allApiScopes {
742 if c.findScopePaths(s) != nil {
743 scopes = append(scopes, s.name)
744 }
745 }
746 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
747 return nil
748 }
749
Paul Duffin23970f42020-05-20 14:20:02 +0100750 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100751}
752
Paul Duffin859fe962020-05-15 10:20:31 +0100753func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
754 componentProps := &struct {
755 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100756 }{}
757
758 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100759 // Mark the stubs library as being components of this java_sdk_library so that
760 // any app that includes code which depends (directly or indirectly) on the stubs
761 // library will have the appropriate <uses-library> invocation inserted into its
762 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100763 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100764 }
765
766 return componentProps
767}
768
Paul Duffindfa131e2020-05-15 20:37:11 +0100769// Check if this can be used as a shared library.
770func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
771 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
772}
773
Paul Duffin859fe962020-05-15 10:20:31 +0100774// Properties related to the use of a module as an component of a java_sdk_library.
775type SdkLibraryComponentProperties struct {
776
777 // The name of the java_sdk_library/_import to add to a <uses-library> entry
778 // in the AndroidManifest.xml of any Android app that includes code that references
779 // this module. If not set then no java_sdk_library/_import is tracked.
780 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
781}
782
783// Structure to be embedded in a module struct that needs to support the
784// SdkLibraryComponentDependency interface.
785type EmbeddableSdkLibraryComponent struct {
786 sdkLibraryComponentProperties SdkLibraryComponentProperties
787}
788
789func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
790 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
791}
792
793// to satisfy SdkLibraryComponentDependency
794func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
795 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
796 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
797 }
798 return nil
799}
800
801// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
802// (including the java_sdk_library) itself.
803type SdkLibraryComponentDependency interface {
804 // The optional name of the sdk library that should be implicitly added to the
805 // AndroidManifest of an app that contains code which references the sdk library.
806 //
807 // Returns an array containing 0 or 1 items rather than a *string to make it easier
808 // to append this to the list of exported sdk libraries.
809 OptionalImplicitSdkLibrary() []string
810}
811
812// Make sure that all the module types that are components of java_sdk_library/_import
813// and which can be referenced (directly or indirectly) from an android app implement
814// the SdkLibraryComponentDependency interface.
815var _ SdkLibraryComponentDependency = (*Library)(nil)
816var _ SdkLibraryComponentDependency = (*Import)(nil)
817var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
818var _ SdkLibraryComponentDependency = (*sdkLibraryImport)(nil)
819
820// Provides access to sdk_version related header and implentation jars.
821type SdkLibraryDependency interface {
822 SdkLibraryComponentDependency
823
824 // Get the header jars appropriate for the supplied sdk_version.
825 //
826 // These are turbine generated jars so they only change if the externals of the
827 // class changes but it does not contain and implementation or JavaDoc.
828 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
829
830 // Get the implementation jars appropriate for the supplied sdk version.
831 //
832 // These are either the implementation jar for the whole sdk library or the implementation
833 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
834 // they are identical to the corresponding header jars.
835 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
836}
837
Inseob Kimc0907f12019-02-08 21:00:45 +0900838type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900839 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900840
Sundong Ahn054b19a2018-10-19 13:46:09 +0900841 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900842
Paul Duffin3375e352020-04-28 10:44:03 +0100843 // Map from api scope to the scope specific property structure.
844 scopeToProperties map[*apiScope]*ApiScopeProperties
845
Paul Duffin56d44902020-01-31 13:36:25 +0000846 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900847}
848
Inseob Kimc0907f12019-02-08 21:00:45 +0900849var _ Dependency = (*SdkLibrary)(nil)
850var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800851
Paul Duffin3375e352020-04-28 10:44:03 +0100852func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
853 return module.sdkLibraryProperties.Generate_system_and_test_apis
854}
855
856func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
857 // Check to see if any scopes have been explicitly enabled. If any have then all
858 // must be.
859 anyScopesExplicitlyEnabled := false
860 for _, scope := range allApiScopes {
861 scopeProperties := module.scopeToProperties[scope]
862 if scopeProperties.Enabled != nil {
863 anyScopesExplicitlyEnabled = true
864 break
865 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000866 }
Paul Duffin3375e352020-04-28 10:44:03 +0100867
868 var generatedScopes apiScopes
869 enabledScopes := make(map[*apiScope]struct{})
870 for _, scope := range allApiScopes {
871 scopeProperties := module.scopeToProperties[scope]
872 // If any scopes are explicitly enabled then ignore the legacy enabled status.
873 // This is to ensure that any new usages of this module type do not rely on legacy
874 // behaviour.
875 defaultEnabledStatus := false
876 if anyScopesExplicitlyEnabled {
877 defaultEnabledStatus = scope.defaultEnabledStatus
878 } else {
879 defaultEnabledStatus = scope.legacyEnabledStatus(module)
880 }
881 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
882 if enabled {
883 enabledScopes[scope] = struct{}{}
884 generatedScopes = append(generatedScopes, scope)
885 }
886 }
887
888 // Now check to make sure that any scope that is extended by an enabled scope is also
889 // enabled.
890 for _, scope := range allApiScopes {
891 if _, ok := enabledScopes[scope]; ok {
892 extends := scope.extends
893 if extends != nil {
894 if _, ok := enabledScopes[extends]; !ok {
895 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
896 }
897 }
898 }
899 }
900
901 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000902}
903
Paul Duffine74ac732020-02-06 13:51:46 +0000904var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
905
Jiyong Parke3833882020-02-17 17:28:10 +0900906func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
907 if dt, ok := depTag.(dependencyTag); ok {
908 return dt == xmlPermissionsFileTag
909 }
910 return false
911}
912
Inseob Kimc0907f12019-02-08 21:00:45 +0900913func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100914 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000915 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100916 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000917
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100918 // If the stubs source and API cannot be generated together then add an additional dependency on
919 // the API module.
920 if apiScope.createStubsSourceAndApiTogether {
921 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinc3091c82020-05-08 14:16:20 +0100922 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100923 } else {
924 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinc3091c82020-05-08 14:16:20 +0100925 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
926 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100927 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900928 }
929
Paul Duffindfa131e2020-05-15 20:37:11 +0100930 if module.requiresRuntimeImplementationLibrary() {
931 if module.sharedLibrary() {
932 // Add dependency to the rule for generating the xml permissions file
933 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
934 }
Paul Duffine74ac732020-02-06 13:51:46 +0000935
Paul Duffindfa131e2020-05-15 20:37:11 +0100936 // Only add the deps for the library if it is actually going to be built.
937 module.Library.deps(ctx)
938 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900939}
940
Paul Duffin46dc45a2020-05-14 15:39:10 +0100941func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
942 paths, err := module.commonOutputFiles(tag)
943 if paths == nil && err == nil {
944 return module.Library.OutputFiles(tag)
945 } else {
946 return paths, err
947 }
948}
949
Inseob Kimc0907f12019-02-08 21:00:45 +0900950func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +0100951 // Only build an implementation library if required.
952 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +0000953 module.Library.GenerateAndroidBuildActions(ctx)
954 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900955
Sundong Ahn57368eb2018-07-06 11:20:23 +0900956 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000957 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900958 // the recorded paths will be returned depending on the link type of the caller.
959 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900960 tag := ctx.OtherModuleDependencyTag(to)
961
Paul Duffinc8782502020-04-29 20:45:27 +0100962 // Extract information from any of the scope specific dependencies.
963 if scopeTag, ok := tag.(scopeDependencyTag); ok {
964 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +0100965 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +0100966
967 // Extract information from the dependency. The exact information extracted
968 // is determined by the nature of the dependency which is determined by the tag.
969 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900970 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900971 })
972}
973
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900974func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +0100975 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +0000976 return nil
977 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900978 entriesList := module.Library.AndroidMkEntries()
979 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700980 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900981 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900982}
983
Jiyong Parkc678ad32018-04-10 13:07:10 +0900984// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900985func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900986 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900987}
988
Jiyong Parkc678ad32018-04-10 13:07:10 +0900989// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900990func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900991 return module.BaseModuleName() + sdkXmlFileSuffix
992}
993
Anton Hansson5fd5d242020-03-27 19:43:19 +0000994// The dist path of the stub artifacts
995func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
996 if module.ModuleBase.Owner() != "" {
997 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
998 } else if Bool(module.sdkLibraryProperties.Core_lib) {
999 return path.Join("apistubs", "core", apiScope.name)
1000 } else {
1001 return path.Join("apistubs", "android", apiScope.name)
1002 }
1003}
1004
Paul Duffin12ceb462019-12-24 20:31:31 +00001005// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001006func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001007 scopeProperties := module.scopeToProperties[apiScope]
1008 if scopeProperties.Sdk_version != nil {
1009 return proptools.String(scopeProperties.Sdk_version)
1010 }
1011
Paul Duffin12ceb462019-12-24 20:31:31 +00001012 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1013 if sdkDep.hasStandardLibs() {
1014 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001015 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001016 } else {
1017 // Otherwise, use no system module.
1018 return "none"
1019 }
1020}
1021
Paul Duffind1b3a922020-01-22 11:57:20 +00001022func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1023 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001024}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001025
Paul Duffind1b3a922020-01-22 11:57:20 +00001026func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1027 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001028}
1029
1030// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001031func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001032 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001033 Name *string
1034 Visibility []string
1035 Srcs []string
1036 Installable *bool
1037 Sdk_version *string
1038 System_modules *string
1039 Patch_module *string
1040 Libs []string
1041 Compile_dex *bool
1042 Java_version *string
1043 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001044 Pdk struct {
1045 Enabled *bool
1046 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001047 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001048 Openjdk9 struct {
1049 Srcs []string
1050 Javacflags []string
1051 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001052 Dist struct {
1053 Targets []string
1054 Dest *string
1055 Dir *string
1056 Tag *string
1057 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001058 }{}
1059
Paul Duffinc3091c82020-05-08 14:16:20 +01001060 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +01001061
1062 // If stubs_library_visibility is not set then the created module will use the
1063 // visibility of this module.
1064 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1065 props.Visibility = visibility
1066
Jiyong Parkc678ad32018-04-10 13:07:10 +09001067 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001068 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001069 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001070 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001071 props.System_modules = module.deviceProperties.System_modules
1072 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001073 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001074 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +09001075 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffina18abc22020-05-16 18:54:24 +01001076 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1077 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
1078 props.Java_version = module.properties.Java_version
1079 if module.deviceProperties.Compile_dex != nil {
1080 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001081 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001082
Anton Hansson5fd5d242020-03-27 19:43:19 +00001083 // Dist the class jar artifact for sdk builds.
1084 if !Bool(module.sdkLibraryProperties.No_dist) {
1085 props.Dist.Targets = []string{"sdk", "win_sdk"}
1086 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1087 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1088 props.Dist.Tag = proptools.StringPtr(".jar")
1089 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001090
Paul Duffin859fe962020-05-15 10:20:31 +01001091 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001092}
1093
Paul Duffin6d0886e2020-04-07 18:49:53 +01001094// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001095// files and also updates and checks the API specification files.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001096func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001097 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001098 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001099 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001100 Srcs []string
1101 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001102 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001103 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001104 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001105 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001106 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001107 Java_version *string
1108 Merge_annotations_dirs []string
1109 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001110 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001111 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001112 Current ApiToCheck
1113 Last_released ApiToCheck
1114 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001115
1116 Api_lint struct {
1117 Enabled *bool
1118 New_since *string
1119 Baseline_file *string
1120 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001121 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001122 Aidl struct {
1123 Include_dirs []string
1124 Local_include_dirs []string
1125 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001126 Dist struct {
1127 Targets []string
1128 Dest *string
1129 Dir *string
1130 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001131 }{}
1132
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001133 // The stubs source processing uses the same compile time classpath when extracting the
1134 // API from the implementation library as it does when compiling it. i.e. the same
1135 // * sdk version
1136 // * system_modules
1137 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001138
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001139 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +01001140
1141 // If stubs_source_visibility is not set then the created module will use the
1142 // visibility of this module.
1143 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1144 props.Visibility = visibility
1145
Paul Duffina18abc22020-05-16 18:54:24 +01001146 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1147 props.Sdk_version = module.deviceProperties.Sdk_version
1148 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001149 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001150 // A droiddoc module has only one Libs property and doesn't distinguish between
1151 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001152 props.Libs = module.properties.Libs
1153 props.Libs = append(props.Libs, module.properties.Static_libs...)
1154 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1155 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1156 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001157
Sundong Ahn054b19a2018-10-19 13:46:09 +09001158 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1159 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1160
Paul Duffin6d0886e2020-04-07 18:49:53 +01001161 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001162 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001163 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001164 }
1165 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001166 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001167 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1168 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001169 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001170 disabledWarnings := []string{
1171 "MissingPermission",
1172 "BroadcastBehavior",
1173 "HiddenSuperclass",
1174 "DeprecationMismatch",
1175 "UnavailableSymbol",
1176 "SdkConstant",
1177 "HiddenTypeParameter",
1178 "Todo",
1179 "Typo",
1180 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001181 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001182
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001183 if !createStubSources {
1184 // Stubs are not required.
1185 props.Generate_stubs = proptools.BoolPtr(false)
1186 }
1187
Paul Duffin1fb487d2020-04-07 18:50:10 +01001188 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001189 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001190 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001191 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001192
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001193 if createApi {
1194 // List of APIs identified from the provided source files are created. They are later
1195 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1196 // last-released (a.k.a numbered) list of API.
1197 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1198 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1199 apiDir := module.getApiDir()
1200 currentApiFileName = path.Join(apiDir, currentApiFileName)
1201 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001202
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001203 // check against the not-yet-release API
1204 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1205 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001206
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001207 if !apiScope.unstable {
1208 // check against the latest released API
1209 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1210 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1211 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1212 module.latestRemovedApiFilegroupName(apiScope))
1213 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001214
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001215 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1216 // Enable api lint.
1217 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1218 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001219
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001220 // If it exists then pass a lint-baseline.txt through to droidstubs.
1221 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1222 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1223 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1224 if err != nil {
1225 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1226 }
1227 if len(paths) == 1 {
1228 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1229 } else if len(paths) != 0 {
1230 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1231 }
Paul Duffin160fe412020-05-10 19:32:20 +01001232 }
1233 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001234
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001235 // Dist the api txt artifact for sdk builds.
1236 if !Bool(module.sdkLibraryProperties.No_dist) {
1237 props.Dist.Targets = []string{"sdk", "win_sdk"}
1238 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1239 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1240 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001241 }
1242
Colin Cross84dfc3d2019-09-25 11:33:01 -07001243 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001244}
1245
Jooyung Han5e9013b2020-03-10 06:23:13 +09001246func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1247 depTag := mctx.OtherModuleDependencyTag(dep)
1248 if depTag == xmlPermissionsFileTag {
1249 return true
1250 }
1251 return module.Library.DepIsInSameApex(mctx, dep)
1252}
1253
Jiyong Parkc678ad32018-04-10 13:07:10 +09001254// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001255func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001256 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001257 Name *string
1258 Lib_name *string
1259 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001260 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +09001261 Name: proptools.StringPtr(module.xmlFileName()),
1262 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1263 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001264 }
Jiyong Parke3833882020-02-17 17:28:10 +09001265
Jiyong Parke3833882020-02-17 17:28:10 +09001266 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001267}
1268
Paul Duffin50061512020-01-21 16:31:05 +00001269func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001270 var ver sdkVersion
1271 var kind sdkKind
1272 if s.usePrebuilt(ctx) {
1273 ver = s.version
1274 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001275 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001276 // We don't have prebuilt SDK for the specific sdkVersion.
1277 // Instead of breaking the build, fallback to use "system_current"
1278 ver = sdkVersionCurrent
1279 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001280 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001281
1282 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001283 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001284 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001285 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001286 if ctx.Config().AllowMissingDependencies() {
1287 return android.Paths{android.PathForSource(ctx, jar)}
1288 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001289 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001290 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001291 return nil
1292 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001293 return android.Paths{jarPath.Path()}
1294}
1295
Paul Duffin9b879592020-05-26 13:21:35 +01001296// Get the apex name for module, "" if it is for platform.
1297func getApexNameForModule(module android.Module) string {
1298 if apex, ok := module.(android.ApexModule); ok {
1299 return apex.ApexName()
1300 }
1301
1302 return ""
1303}
1304
1305// Check to see if the other module is within the same named APEX as this module.
1306//
1307// If either this or the other module are on the platform then this will return
1308// false.
1309func (module *SdkLibrary) withinSameApexAs(other android.Module) bool {
1310 name := module.ApexName()
1311 return name != "" && getApexNameForModule(other) == name
1312}
1313
Paul Duffinb05d4292020-05-20 12:19:10 +01001314func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001315
Paul Duffindaaa3322020-05-26 18:13:57 +01001316 // Only provide access to the implementation library if it is actually built.
1317 if module.requiresRuntimeImplementationLibrary() {
1318 // Check any special cases for java_sdk_library.
1319 //
1320 // Only allow access to the implementation library in the following condition:
1321 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001322 // * The referencing module is in the same apex as this.
1323 if sdkVersion.kind == sdkPrivate || module.withinSameApexAs(ctx.Module()) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001324 if headerJars {
1325 return module.HeaderJars()
1326 } else {
1327 return module.ImplementationJars()
1328 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001329 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001330 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001331
Paul Duffin23970f42020-05-20 14:20:02 +01001332 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001333}
1334
Sundong Ahn241cd372018-07-13 16:16:44 +09001335// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001336func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1337 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1338}
1339
1340// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001341func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001342 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001343}
1344
Sundong Ahn80a87b32019-05-13 15:02:50 +09001345func (module *SdkLibrary) SetNoDist() {
1346 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1347}
1348
Colin Cross571cccf2019-02-04 11:22:08 -08001349var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1350
Jiyong Park82484c02018-04-23 21:41:26 +09001351func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001352 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001353 return &[]string{}
1354 }).(*[]string)
1355}
1356
Paul Duffin749f98f2019-12-30 17:23:46 +00001357func (module *SdkLibrary) getApiDir() string {
1358 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1359}
1360
Jiyong Parkc678ad32018-04-10 13:07:10 +09001361// For a java_sdk_library module, create internal modules for stubs, docs,
1362// runtime libs and xml file. If requested, the stubs and docs are created twice
1363// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001364func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1365 // If the module has been disabled then don't create any child modules.
1366 if !module.Enabled() {
1367 return
1368 }
1369
Paul Duffina18abc22020-05-16 18:54:24 +01001370 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001371 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001372 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001373 }
1374
Paul Duffin37e0b772019-12-30 17:20:10 +00001375 // If this builds against standard libraries (i.e. is not part of the core libraries)
1376 // then assume it provides both system and test apis. Otherwise, assume it does not and
1377 // also assume it does not contribute to the dist build.
1378 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1379 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001380 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001381 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1382
Inseob Kim8098faa2019-03-18 10:19:51 +09001383 missing_current_api := false
1384
Paul Duffin3375e352020-04-28 10:44:03 +01001385 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001386
Paul Duffin749f98f2019-12-30 17:23:46 +00001387 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001388 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001389 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001390 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001391 p := android.ExistentPathForSource(mctx, path)
1392 if !p.Valid() {
1393 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1394 missing_current_api = true
1395 }
1396 }
1397 }
1398
1399 if missing_current_api {
1400 script := "build/soong/scripts/gen-java-current-api-files.sh"
1401 p := android.ExistentPathForSource(mctx, script)
1402
1403 if !p.Valid() {
1404 panic(fmt.Sprintf("script file %s doesn't exist", script))
1405 }
1406
1407 mctx.ModuleErrorf("One or more current api files are missing. "+
1408 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001409 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001410 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001411 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001412 return
1413 }
1414
Paul Duffin3375e352020-04-28 10:44:03 +01001415 for _, scope := range generatedScopes {
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001416 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinc3091c82020-05-08 14:16:20 +01001417 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001418
1419 // If the args needed to generate the stubs and API are the same then they
1420 // can be generated in a single invocation of metalava, otherwise they will
1421 // need separate invocations.
1422 if scope.createStubsSourceAndApiTogether {
1423 // Use the stubs source name for legacy reasons.
1424 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1425 } else {
1426 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1427
1428 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinc3091c82020-05-08 14:16:20 +01001429 apiName := module.apiModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001430 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1431 }
1432
Paul Duffind1b3a922020-01-22 11:57:20 +00001433 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001434 }
1435
Paul Duffindfa131e2020-05-15 20:37:11 +01001436 if module.requiresRuntimeImplementationLibrary() {
1437 // Only create an XML permissions file that declares the library as being usable
1438 // as a shared library if required.
1439 if module.sharedLibrary() {
1440 module.createXmlFile(mctx)
1441 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001442
1443 // record java_sdk_library modules so that they are exported to make
1444 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1445 javaSdkLibrariesLock.Lock()
1446 defer javaSdkLibrariesLock.Unlock()
1447 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1448 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001449}
1450
1451func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001452 module.AddProperties(
1453 &module.sdkLibraryProperties,
Paul Duffina18abc22020-05-16 18:54:24 +01001454 &module.properties,
1455 &module.dexpreoptProperties,
1456 &module.deviceProperties,
1457 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001458 )
1459
Paul Duffin859fe962020-05-15 10:20:31 +01001460 module.initSdkLibraryComponent(&module.ModuleBase)
1461
Paul Duffina18abc22020-05-16 18:54:24 +01001462 module.properties.Installable = proptools.BoolPtr(true)
1463 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001464}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001465
Paul Duffindfa131e2020-05-15 20:37:11 +01001466func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1467 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1468}
1469
Paul Duffin1b1e8062020-05-08 13:44:43 +01001470// Defines how to name the individual component modules the sdk library creates.
1471type sdkLibraryComponentNamingScheme interface {
1472 stubsLibraryModuleName(scope *apiScope, baseName string) string
1473
1474 stubsSourceModuleName(scope *apiScope, baseName string) string
1475
1476 apiModuleName(scope *apiScope, baseName string) string
1477}
1478
1479type defaultNamingScheme struct {
1480}
1481
1482func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1483 return scope.stubsLibraryModuleName(baseName)
1484}
1485
1486func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1487 return scope.stubsSourceModuleName(baseName)
1488}
1489
1490func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1491 return scope.apiModuleName(baseName)
1492}
1493
1494var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1495
Paul Duffin6c9c5fc2020-05-08 15:36:30 +01001496type frameworkModulesNamingScheme struct {
1497}
1498
1499func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1500 suffix := scope.name
1501 if scope == apiScopeModuleLib {
1502 suffix = "module_libs_"
1503 }
1504 return suffix
1505}
1506
1507func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1508 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1509}
1510
1511func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1512 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1513}
1514
1515func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1516 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1517}
1518
1519var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1520
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001521// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1522// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1523// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1524// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1525// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001526func SdkLibraryFactory() android.Module {
1527 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001528
1529 // Initialize information common between source and prebuilt.
1530 module.initCommon(&module.ModuleBase)
1531
Inseob Kimc0907f12019-02-08 21:00:45 +09001532 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001533 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001534 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001535
1536 // Initialize the map from scope to scope specific properties.
1537 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1538 for _, scope := range allApiScopes {
1539 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1540 }
1541 module.scopeToProperties = scopeToProperties
1542
Paul Duffin4911a892020-04-29 23:35:13 +01001543 // Add the properties containing visibility rules so that they are checked.
1544 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1545 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1546
Paul Duffin1b1e8062020-05-08 13:44:43 +01001547 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001548 // If no implementation is required then it cannot be used as a shared library
1549 // either.
1550 if !module.requiresRuntimeImplementationLibrary() {
1551 // If shared_library has been explicitly set to true then it is incompatible
1552 // with api_only: true.
1553 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1554 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1555 }
1556 // Set shared_library: false.
1557 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1558 }
1559
Paul Duffin1b1e8062020-05-08 13:44:43 +01001560 if module.initCommonAfterDefaultsApplied(ctx) {
1561 module.CreateInternalModules(ctx)
1562 }
1563 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001564 return module
1565}
Colin Cross79c7c262019-04-17 11:11:46 -07001566
1567//
1568// SDK library prebuilts
1569//
1570
Paul Duffin56d44902020-01-31 13:36:25 +00001571// Properties associated with each api scope.
1572type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001573 Jars []string `android:"path"`
1574
1575 Sdk_version *string
1576
Colin Cross79c7c262019-04-17 11:11:46 -07001577 // List of shared java libs that this module has dependencies to
1578 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001579
Paul Duffinc8782502020-04-29 20:45:27 +01001580 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001581 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001582
1583 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001584 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001585
1586 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001587 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001588}
1589
Paul Duffin56d44902020-01-31 13:36:25 +00001590type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001591 // List of shared java libs, common to all scopes, that this module has
1592 // dependencies to
1593 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001594}
1595
Colin Cross79c7c262019-04-17 11:11:46 -07001596type sdkLibraryImport struct {
1597 android.ModuleBase
1598 android.DefaultableModuleBase
1599 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001600 android.ApexModuleBase
1601 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001602
1603 properties sdkLibraryImportProperties
1604
Paul Duffin46a26a82020-04-07 19:27:04 +01001605 // Map from api scope to the scope specific property structure.
1606 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1607
Paul Duffin56d44902020-01-31 13:36:25 +00001608 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001609}
1610
1611var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1612
Paul Duffin46a26a82020-04-07 19:27:04 +01001613// The type of a structure that contains a field of type sdkLibraryScopeProperties
1614// for each apiscope in allApiScopes, e.g. something like:
1615// struct {
1616// Public sdkLibraryScopeProperties
1617// System sdkLibraryScopeProperties
1618// ...
1619// }
1620var allScopeStructType = createAllScopePropertiesStructType()
1621
1622// Dynamically create a structure type for each apiscope in allApiScopes.
1623func createAllScopePropertiesStructType() reflect.Type {
1624 var fields []reflect.StructField
1625 for _, apiScope := range allApiScopes {
1626 field := reflect.StructField{
1627 Name: apiScope.fieldName,
1628 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1629 }
1630 fields = append(fields, field)
1631 }
1632
1633 return reflect.StructOf(fields)
1634}
1635
1636// Create an instance of the scope specific structure type and return a map
1637// from apiscope to a pointer to each scope specific field.
1638func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1639 allScopePropertiesPtr := reflect.New(allScopeStructType)
1640 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1641 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1642
1643 for _, apiScope := range allApiScopes {
1644 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1645 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1646 }
1647
1648 return allScopePropertiesPtr.Interface(), scopeProperties
1649}
1650
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001651// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001652func sdkLibraryImportFactory() android.Module {
1653 module := &sdkLibraryImport{}
1654
Paul Duffin46a26a82020-04-07 19:27:04 +01001655 allScopeProperties, scopeToProperties := createPropertiesInstance()
1656 module.scopeProperties = scopeToProperties
1657 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001658
Paul Duffinc3091c82020-05-08 14:16:20 +01001659 // Initialize information common between source and prebuilt.
1660 module.initCommon(&module.ModuleBase)
1661
Paul Duffin0bdcb272020-02-06 15:24:57 +00001662 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001663 android.InitApexModule(module)
1664 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001665 InitJavaModule(module, android.HostAndDeviceSupported)
1666
Paul Duffin1b1e8062020-05-08 13:44:43 +01001667 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1668 if module.initCommonAfterDefaultsApplied(mctx) {
1669 module.createInternalModules(mctx)
1670 }
1671 })
Colin Cross79c7c262019-04-17 11:11:46 -07001672 return module
1673}
1674
1675func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1676 return &module.prebuilt
1677}
1678
1679func (module *sdkLibraryImport) Name() string {
1680 return module.prebuilt.Name(module.ModuleBase.Name())
1681}
1682
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001683func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001684
Paul Duffin50061512020-01-21 16:31:05 +00001685 // If the build is configured to use prebuilts then force this to be preferred.
1686 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1687 module.prebuilt.ForcePrefer()
1688 }
1689
Paul Duffin46a26a82020-04-07 19:27:04 +01001690 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001691 if len(scopeProperties.Jars) == 0 {
1692 continue
1693 }
1694
Paul Duffinbbb546b2020-04-09 00:07:11 +01001695 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001696
Paul Duffin0f8faff2020-05-20 16:18:00 +01001697 if len(scopeProperties.Stub_srcs) > 0 {
1698 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1699 }
Paul Duffin56d44902020-01-31 13:36:25 +00001700 }
Colin Cross79c7c262019-04-17 11:11:46 -07001701
1702 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1703 javaSdkLibrariesLock.Lock()
1704 defer javaSdkLibrariesLock.Unlock()
1705 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1706}
1707
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001708func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001709 // Creates a java import for the jar with ".stubs" suffix
1710 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001711 Name *string
1712 Sdk_version *string
1713 Libs []string
1714 Jars []string
1715 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001716 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001717 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001718 props.Sdk_version = scopeProperties.Sdk_version
1719 // Prepend any of the libs from the legacy public properties to the libs for each of the
1720 // scopes to avoid having to duplicate them in each scope.
1721 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1722 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001723
Paul Duffin38b57852020-05-13 16:08:09 +01001724 // The imports are preferred if the java_sdk_library_import is preferred.
1725 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001726
1727 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001728}
1729
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001730func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001731 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001732 Name *string
1733 Srcs []string
1734 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001735 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001736 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001737 props.Srcs = scopeProperties.Stub_srcs
1738 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001739
1740 // The stubs source is preferred if the java_sdk_library_import is preferred.
1741 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001742}
1743
Colin Cross79c7c262019-04-17 11:11:46 -07001744func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001745 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001746 if len(scopeProperties.Jars) == 0 {
1747 continue
1748 }
1749
1750 // Add dependencies to the prebuilt stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001751 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001752
1753 if len(scopeProperties.Stub_srcs) > 0 {
1754 // Add dependencies to the prebuilt stubs source library
1755 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1756 }
Paul Duffin56d44902020-01-31 13:36:25 +00001757 }
Colin Cross79c7c262019-04-17 11:11:46 -07001758}
1759
Paul Duffin46dc45a2020-05-14 15:39:10 +01001760func (module *sdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
1761 return module.commonOutputFiles(tag)
1762}
1763
Colin Cross79c7c262019-04-17 11:11:46 -07001764func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin0f8faff2020-05-20 16:18:00 +01001765 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001766 ctx.VisitDirectDeps(func(to android.Module) {
1767 tag := ctx.OtherModuleDependencyTag(to)
1768
Paul Duffin0f8faff2020-05-20 16:18:00 +01001769 // Extract information from any of the scope specific dependencies.
1770 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1771 apiScope := scopeTag.apiScope
1772 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1773
1774 // Extract information from the dependency. The exact information extracted
1775 // is determined by the nature of the dependency which is determined by the tag.
1776 scopeTag.extractDepInfo(ctx, to, scopePaths)
Colin Cross79c7c262019-04-17 11:11:46 -07001777 }
1778 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001779
1780 // Populate the scope paths with information from the properties.
1781 for apiScope, scopeProperties := range module.scopeProperties {
1782 if len(scopeProperties.Jars) == 0 {
1783 continue
1784 }
1785
1786 paths := module.getScopePathsCreateIfNeeded(apiScope)
1787 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1788 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1789 }
Colin Cross79c7c262019-04-17 11:11:46 -07001790}
1791
Paul Duffinb05d4292020-05-20 12:19:10 +01001792func (module *sdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin23970f42020-05-20 14:20:02 +01001793 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001794}
1795
Colin Cross79c7c262019-04-17 11:11:46 -07001796// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001797func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001798 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001799 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001800}
1801
1802// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001803func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001804 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001805 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001806}
Jiyong Parke3833882020-02-17 17:28:10 +09001807
1808//
1809// java_sdk_library_xml
1810//
1811type sdkLibraryXml struct {
1812 android.ModuleBase
1813 android.DefaultableModuleBase
1814 android.ApexModuleBase
1815
1816 properties sdkLibraryXmlProperties
1817
1818 outputFilePath android.OutputPath
1819 installDirPath android.InstallPath
1820}
1821
1822type sdkLibraryXmlProperties struct {
1823 // canonical name of the lib
1824 Lib_name *string
1825}
1826
1827// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1828// Not to be used directly by users. java_sdk_library internally uses this.
1829func sdkLibraryXmlFactory() android.Module {
1830 module := &sdkLibraryXml{}
1831
1832 module.AddProperties(&module.properties)
1833
1834 android.InitApexModule(module)
1835 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1836
1837 return module
1838}
1839
1840// from android.PrebuiltEtcModule
1841func (module *sdkLibraryXml) SubDir() string {
1842 return "permissions"
1843}
1844
1845// from android.PrebuiltEtcModule
1846func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1847 return module.outputFilePath
1848}
1849
1850// from android.ApexModule
1851func (module *sdkLibraryXml) AvailableFor(what string) bool {
1852 return true
1853}
1854
1855func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1856 // do nothing
1857}
1858
1859// File path to the runtime implementation library
1860func (module *sdkLibraryXml) implPath() string {
1861 implName := proptools.String(module.properties.Lib_name)
1862 if apexName := module.ApexName(); apexName != "" {
1863 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1864 // In most cases, this works fine. But when apex_name is set or override_apex is used
1865 // this can be wrong.
1866 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1867 }
1868 partition := "system"
1869 if module.SocSpecific() {
1870 partition = "vendor"
1871 } else if module.DeviceSpecific() {
1872 partition = "odm"
1873 } else if module.ProductSpecific() {
1874 partition = "product"
1875 } else if module.SystemExtSpecific() {
1876 partition = "system_ext"
1877 }
1878 return "/" + partition + "/framework/" + implName + ".jar"
1879}
1880
1881func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1882 libName := proptools.String(module.properties.Lib_name)
1883 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1884
1885 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1886 rule := android.NewRuleBuilder()
1887 rule.Command().
1888 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1889 Output(module.outputFilePath)
1890
1891 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1892
1893 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1894}
1895
1896func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1897 if !module.IsForPlatform() {
1898 return []android.AndroidMkEntries{android.AndroidMkEntries{
1899 Disabled: true,
1900 }}
1901 }
1902
1903 return []android.AndroidMkEntries{android.AndroidMkEntries{
1904 Class: "ETC",
1905 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1906 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1907 func(entries *android.AndroidMkEntries) {
1908 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1909 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1910 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1911 },
1912 },
1913 }}
1914}
Paul Duffindd46f712020-02-10 13:37:10 +00001915
1916type sdkLibrarySdkMemberType struct {
1917 android.SdkMemberTypeBase
1918}
1919
1920func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1921 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1922}
1923
1924func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1925 _, ok := module.(*SdkLibrary)
1926 return ok
1927}
1928
1929func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1930 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1931}
1932
1933func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1934 return &sdkLibrarySdkMemberProperties{}
1935}
1936
1937type sdkLibrarySdkMemberProperties struct {
1938 android.SdkMemberPropertiesBase
1939
1940 // Scope to per scope properties.
1941 Scopes map[*apiScope]scopeProperties
1942
1943 // Additional libraries that the exported stubs libraries depend upon.
1944 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001945
1946 // The Java stubs source files.
1947 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01001948
1949 // The naming scheme.
1950 Naming_scheme *string
Paul Duffindd46f712020-02-10 13:37:10 +00001951}
1952
1953type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001954 Jars android.Paths
1955 StubsSrcJar android.Path
1956 CurrentApiFile android.Path
1957 RemovedApiFile android.Path
1958 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001959}
1960
1961func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1962 sdk := variant.(*SdkLibrary)
1963
1964 s.Scopes = make(map[*apiScope]scopeProperties)
1965 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001966 paths := sdk.findScopePaths(apiScope)
1967 if paths == nil {
1968 continue
1969 }
1970
Paul Duffindd46f712020-02-10 13:37:10 +00001971 jars := paths.stubsImplPath
1972 if len(jars) > 0 {
1973 properties := scopeProperties{}
1974 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01001975 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01001976 properties.StubsSrcJar = paths.stubsSrcJar.Path()
1977 properties.CurrentApiFile = paths.currentApiFilePath.Path()
1978 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffindd46f712020-02-10 13:37:10 +00001979 s.Scopes[apiScope] = properties
1980 }
1981 }
1982
1983 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01001984 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffindd46f712020-02-10 13:37:10 +00001985}
1986
1987func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01001988 if s.Naming_scheme != nil {
1989 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
1990 }
1991
Paul Duffindd46f712020-02-10 13:37:10 +00001992 for _, apiScope := range allApiScopes {
1993 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01001994 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00001995
Paul Duffin3d1248c2020-04-09 00:10:17 +01001996 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1997
Paul Duffindd46f712020-02-10 13:37:10 +00001998 var jars []string
1999 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002000 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002001 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2002 jars = append(jars, dest)
2003 }
2004 scopeSet.AddProperty("jars", jars)
2005
Paul Duffin3d1248c2020-04-09 00:10:17 +01002006 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2007 // the source files are also unpacked.
2008 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2009 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2010 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2011
Paul Duffin1fd005d2020-04-09 01:08:11 +01002012 if properties.CurrentApiFile != nil {
2013 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2014 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2015 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2016 }
2017
2018 if properties.RemovedApiFile != nil {
2019 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
2020 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
2021 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2022 }
2023
Paul Duffindd46f712020-02-10 13:37:10 +00002024 if properties.SdkVersion != "" {
2025 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2026 }
2027 }
2028 }
2029
2030 if len(s.Libs) > 0 {
2031 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2032 }
2033}