blob: 237be1076f6a1cf090a9574737fad7c397942ade [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 Duffin5df79302020-05-16 15:52:12 +0100351 // Visibility for impl library module. If not specified then defaults to the
352 // visibility property.
353 Impl_library_visibility []string
354
Paul Duffin4911a892020-04-29 23:35:13 +0100355 // Visibility for stubs library modules. If not specified then defaults to the
356 // visibility property.
357 Stubs_library_visibility []string
358
359 // Visibility for stubs source modules. If not specified then defaults to the
360 // visibility property.
361 Stubs_source_visibility []string
362
Sundong Ahnf043cf62018-06-25 16:04:37 +0900363 // List of Java libraries that will be in the classpath when building stubs
364 Stub_only_libs []string `android:"arch_variant"`
365
Paul Duffin7a586d32019-12-30 17:09:34 +0000366 // list of package names that will be documented and publicized as API.
367 // This allows the API to be restricted to a subset of the source files provided.
368 // If this is unspecified then all the source files will be treated as being part
369 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900370 Api_packages []string
371
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900372 // list of package names that must be hidden from the API
373 Hidden_api_packages []string
374
Paul Duffin749f98f2019-12-30 17:23:46 +0000375 // the relative path to the directory containing the api specification files.
376 // Defaults to "api".
377 Api_dir *string
378
Paul Duffindfa131e2020-05-15 20:37:11 +0100379 // Determines whether a runtime implementation library is built; defaults to false.
380 //
381 // If true then it also prevents the module from being used as a shared module, i.e.
382 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000383 Api_only *bool
384
Paul Duffin11512472019-02-11 15:55:17 +0000385 // local files that are used within user customized droiddoc options.
386 Droiddoc_option_files []string
387
388 // additional droiddoc options
389 // Available variables for substitution:
390 //
391 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900392 Droiddoc_options []string
393
Sundong Ahn054b19a2018-10-19 13:46:09 +0900394 // a list of top-level directories containing files to merge qualifier annotations
395 // (i.e. those intended to be included in the stubs written) from.
396 Merge_annotations_dirs []string
397
398 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
399 Merge_inclusion_annotations_dirs []string
400
401 // If set to true, the path of dist files is apistubs/core. Defaults to false.
402 Core_lib *bool
403
Sundong Ahn80a87b32019-05-13 15:02:50 +0900404 // don't create dist rules.
405 No_dist *bool `blueprint:"mutated"`
406
Paul Duffin3375e352020-04-28 10:44:03 +0100407 // indicates whether system and test apis should be generated.
408 Generate_system_and_test_apis bool `blueprint:"mutated"`
409
410 // The properties specific to the public api scope
411 //
412 // Unless explicitly specified by using public.enabled the public api scope is
413 // enabled by default in both legacy and non-legacy mode.
414 Public ApiScopeProperties
415
416 // The properties specific to the system api scope
417 //
418 // In legacy mode the system api scope is enabled by default when sdk_version
419 // is set to something other than "none".
420 //
421 // In non-legacy mode the system api scope is disabled by default.
422 System ApiScopeProperties
423
424 // The properties specific to the test api scope
425 //
426 // In legacy mode the test api scope is enabled by default when sdk_version
427 // is set to something other than "none".
428 //
429 // In non-legacy mode the test api scope is disabled by default.
430 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000431
Paul Duffin8f265b92020-04-28 14:13:56 +0100432 // The properties specific to the module_lib api scope
433 //
434 // Unless explicitly specified by using test.enabled the module_lib api scope is
435 // disabled by default.
436 Module_lib ApiScopeProperties
437
Paul Duffin160fe412020-05-10 19:32:20 +0100438 // Properties related to api linting.
439 Api_lint struct {
440 // Enable api linting.
441 Enabled *bool
442 }
443
Jiyong Parkc678ad32018-04-10 13:07:10 +0900444 // TODO: determines whether to create HTML doc or not
445 //Html_doc *bool
446}
447
Paul Duffin0f8faff2020-05-20 16:18:00 +0100448// Paths to outputs from java_sdk_library and java_sdk_library_import.
449//
450// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
451// OptionalPaths are always set by java_sdk_library but may not be set by
452// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000453type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100454 // The path (represented as Paths for convenience when returning) to the stubs header jar.
455 //
456 // That is the jar that is created by turbine.
457 stubsHeaderPath android.Paths
458
459 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
460 //
461 // This is not the implementation jar, it still only contains stubs.
462 stubsImplPath android.Paths
463
464 // The API specification file, e.g. system_current.txt.
465 currentApiFilePath android.OptionalPath
466
467 // The specification of API elements removed since the last release.
468 removedApiFilePath android.OptionalPath
469
470 // The stubs source jar.
471 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000472}
473
Paul Duffinc8782502020-04-29 20:45:27 +0100474func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
475 if lib, ok := dep.(Dependency); ok {
476 paths.stubsHeaderPath = lib.HeaderJars()
477 paths.stubsImplPath = lib.ImplementationJars()
478 return nil
479 } else {
480 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
481 }
482}
483
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100484func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
485 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
486 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100487 return nil
488 } else {
489 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
490 }
491}
492
Paul Duffin0f8faff2020-05-20 16:18:00 +0100493func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
494 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
495 action(apiStubsProvider)
496 return nil
497 } else {
498 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
499 }
500}
501
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100502func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100503 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
504 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100505}
506
507func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
508 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
509 paths.extractApiInfoFromApiStubsProvider(provider)
510 })
511}
512
Paul Duffin0f8faff2020-05-20 16:18:00 +0100513func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
514 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100515}
516
517func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100518 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100519 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
520 })
521}
522
523func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
524 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
525 paths.extractApiInfoFromApiStubsProvider(provider)
526 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
527 })
528}
529
530type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100531 // The naming scheme to use for the components that this module creates.
532 //
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100533 // If not specified then it defaults to "default". The other allowable value is
534 // "framework-modules" which matches the scheme currently used by framework modules
535 // for the equivalent components represented as separate Soong modules.
Paul Duffin1b1e8062020-05-08 13:44:43 +0100536 //
537 // This is a temporary mechanism to simplify conversion from separate modules for each
538 // component that follow a different naming pattern to the default one.
539 //
540 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100541 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100542
543 // Specifies whether this module can be used as an Android shared library; defaults
544 // to true.
545 //
546 // An Android shared library is one that can be referenced in a <uses-library> element
547 // in an AndroidManifest.xml.
548 Shared_library *bool
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100549}
550
Paul Duffin56d44902020-01-31 13:36:25 +0000551// Common code between sdk library and sdk library import
552type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100553 moduleBase *android.ModuleBase
554
Paul Duffin56d44902020-01-31 13:36:25 +0000555 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100556
557 namingScheme sdkLibraryComponentNamingScheme
558
Paul Duffindfa131e2020-05-15 20:37:11 +0100559 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100560
561 // Functionality related to this being used as a component of a java_sdk_library.
562 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000563}
564
Paul Duffinc3091c82020-05-08 14:16:20 +0100565func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
566 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100567
Paul Duffindfa131e2020-05-15 20:37:11 +0100568 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100569
570 // Initialize this as an sdk library component.
571 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100572}
573
574func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100575 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100576 switch schemeProperty {
577 case "default":
578 c.namingScheme = &defaultNamingScheme{}
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100579 case "framework-modules":
580 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1b1e8062020-05-08 13:44:43 +0100581 default:
582 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
583 return false
584 }
585
Paul Duffindfa131e2020-05-15 20:37:11 +0100586 // Only track this sdk library if this can be used as a shared library.
587 if c.sharedLibrary() {
588 // Use the name specified in the module definition as the owner.
589 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
590 }
Paul Duffin859fe962020-05-15 10:20:31 +0100591
Paul Duffin1b1e8062020-05-08 13:44:43 +0100592 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100593}
594
595// Name of the java_library module that compiles the stubs source.
596func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100597 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100598}
599
600// Name of the droidstubs module that generates the stubs source and may also
601// generate/check the API.
602func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100603 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100604}
605
606// Name of the droidstubs module that generates/checks the API. Only used if it
607// requires different arts to the stubs source generating module.
608func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100609 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100610}
611
Paul Duffin46dc45a2020-05-14 15:39:10 +0100612// The component names for different outputs of the java_sdk_library.
613//
614// They are similar to the names used for the child modules it creates
615const (
616 stubsSourceComponentName = "stubs.source"
617
618 apiTxtComponentName = "api.txt"
619
620 removedApiTxtComponentName = "removed-api.txt"
621)
622
623// A regular expression to match tags that reference a specific stubs component.
624//
625// It will only match if given a valid scope and a valid component. It is verfy strict
626// to ensure it does not accidentally match a similar looking tag that should be processed
627// by the embedded Library.
628var tagSplitter = func() *regexp.Regexp {
629 // Given a list of literal string items returns a regular expression that will
630 // match any one of the items.
631 choice := func(items ...string) string {
632 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
633 }
634
635 // Regular expression to match one of the scopes.
636 scopesRegexp := choice(allScopeNames...)
637
638 // Regular expression to match one of the components.
639 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
640
641 // Regular expression to match any combination of one scope and one component.
642 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
643}()
644
645// For OutputFileProducer interface
646//
647// .<scope>.stubs.source
648// .<scope>.api.txt
649// .<scope>.removed-api.txt
650func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
651 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
652 scopeName := groups[1]
653 component := groups[2]
654
655 if scope, ok := scopeByName[scopeName]; ok {
656 paths := c.findScopePaths(scope)
657 if paths == nil {
658 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
659 }
660
661 switch component {
662 case stubsSourceComponentName:
663 if paths.stubsSrcJar.Valid() {
664 return android.Paths{paths.stubsSrcJar.Path()}, nil
665 }
666
667 case apiTxtComponentName:
668 if paths.currentApiFilePath.Valid() {
669 return android.Paths{paths.currentApiFilePath.Path()}, nil
670 }
671
672 case removedApiTxtComponentName:
673 if paths.removedApiFilePath.Valid() {
674 return android.Paths{paths.removedApiFilePath.Path()}, nil
675 }
676 }
677
678 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
679 } else {
680 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
681 }
682
683 } else {
684 return nil, nil
685 }
686}
687
Paul Duffin803a9562020-05-20 11:52:25 +0100688func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000689 if c.scopePaths == nil {
690 c.scopePaths = make(map[*apiScope]*scopePaths)
691 }
692 paths := c.scopePaths[scope]
693 if paths == nil {
694 paths = &scopePaths{}
695 c.scopePaths[scope] = paths
696 }
697
698 return paths
699}
700
Paul Duffin803a9562020-05-20 11:52:25 +0100701func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
702 if c.scopePaths == nil {
703 return nil
704 }
705
706 return c.scopePaths[scope]
707}
708
709// If this does not support the requested api scope then find the closest available
710// scope it does support. Returns nil if no such scope is available.
711func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
712 for s := scope; s != nil; s = s.extends {
713 if paths := c.findScopePaths(s); paths != nil {
714 return paths
715 }
716 }
717
718 // This should never happen outside tests as public should be the base scope for every
719 // scope and is enabled by default.
720 return nil
721}
722
Paul Duffin23970f42020-05-20 14:20:02 +0100723func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100724
725 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
726 if sdkVersion.version.isNumbered() {
727 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
728 }
729
730 var apiScope *apiScope
731 switch sdkVersion.kind {
732 case sdkSystem:
733 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100734 case sdkModule:
735 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100736 case sdkTest:
737 apiScope = apiScopeTest
738 default:
739 apiScope = apiScopePublic
740 }
741
Paul Duffin803a9562020-05-20 11:52:25 +0100742 paths := c.findClosestScopePath(apiScope)
743 if paths == nil {
744 var scopes []string
745 for _, s := range allApiScopes {
746 if c.findScopePaths(s) != nil {
747 scopes = append(scopes, s.name)
748 }
749 }
750 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
751 return nil
752 }
753
Paul Duffin23970f42020-05-20 14:20:02 +0100754 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100755}
756
Paul Duffin859fe962020-05-15 10:20:31 +0100757func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
758 componentProps := &struct {
759 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100760 }{}
761
762 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100763 // Mark the stubs library as being components of this java_sdk_library so that
764 // any app that includes code which depends (directly or indirectly) on the stubs
765 // library will have the appropriate <uses-library> invocation inserted into its
766 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100767 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100768 }
769
770 return componentProps
771}
772
Paul Duffindfa131e2020-05-15 20:37:11 +0100773// Check if this can be used as a shared library.
774func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
775 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
776}
777
Paul Duffin859fe962020-05-15 10:20:31 +0100778// Properties related to the use of a module as an component of a java_sdk_library.
779type SdkLibraryComponentProperties struct {
780
781 // The name of the java_sdk_library/_import to add to a <uses-library> entry
782 // in the AndroidManifest.xml of any Android app that includes code that references
783 // this module. If not set then no java_sdk_library/_import is tracked.
784 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
785}
786
787// Structure to be embedded in a module struct that needs to support the
788// SdkLibraryComponentDependency interface.
789type EmbeddableSdkLibraryComponent struct {
790 sdkLibraryComponentProperties SdkLibraryComponentProperties
791}
792
793func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
794 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
795}
796
797// to satisfy SdkLibraryComponentDependency
798func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
799 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
800 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
801 }
802 return nil
803}
804
805// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
806// (including the java_sdk_library) itself.
807type SdkLibraryComponentDependency interface {
808 // The optional name of the sdk library that should be implicitly added to the
809 // AndroidManifest of an app that contains code which references the sdk library.
810 //
811 // Returns an array containing 0 or 1 items rather than a *string to make it easier
812 // to append this to the list of exported sdk libraries.
813 OptionalImplicitSdkLibrary() []string
814}
815
816// Make sure that all the module types that are components of java_sdk_library/_import
817// and which can be referenced (directly or indirectly) from an android app implement
818// the SdkLibraryComponentDependency interface.
819var _ SdkLibraryComponentDependency = (*Library)(nil)
820var _ SdkLibraryComponentDependency = (*Import)(nil)
821var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
822var _ SdkLibraryComponentDependency = (*sdkLibraryImport)(nil)
823
824// Provides access to sdk_version related header and implentation jars.
825type SdkLibraryDependency interface {
826 SdkLibraryComponentDependency
827
828 // Get the header jars appropriate for the supplied sdk_version.
829 //
830 // These are turbine generated jars so they only change if the externals of the
831 // class changes but it does not contain and implementation or JavaDoc.
832 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
833
834 // Get the implementation jars appropriate for the supplied sdk version.
835 //
836 // These are either the implementation jar for the whole sdk library or the implementation
837 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
838 // they are identical to the corresponding header jars.
839 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
840}
841
Inseob Kimc0907f12019-02-08 21:00:45 +0900842type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900843 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900844
Sundong Ahn054b19a2018-10-19 13:46:09 +0900845 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900846
Paul Duffin3375e352020-04-28 10:44:03 +0100847 // Map from api scope to the scope specific property structure.
848 scopeToProperties map[*apiScope]*ApiScopeProperties
849
Paul Duffin56d44902020-01-31 13:36:25 +0000850 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900851}
852
Inseob Kimc0907f12019-02-08 21:00:45 +0900853var _ Dependency = (*SdkLibrary)(nil)
854var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800855
Paul Duffin3375e352020-04-28 10:44:03 +0100856func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
857 return module.sdkLibraryProperties.Generate_system_and_test_apis
858}
859
860func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
861 // Check to see if any scopes have been explicitly enabled. If any have then all
862 // must be.
863 anyScopesExplicitlyEnabled := false
864 for _, scope := range allApiScopes {
865 scopeProperties := module.scopeToProperties[scope]
866 if scopeProperties.Enabled != nil {
867 anyScopesExplicitlyEnabled = true
868 break
869 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000870 }
Paul Duffin3375e352020-04-28 10:44:03 +0100871
872 var generatedScopes apiScopes
873 enabledScopes := make(map[*apiScope]struct{})
874 for _, scope := range allApiScopes {
875 scopeProperties := module.scopeToProperties[scope]
876 // If any scopes are explicitly enabled then ignore the legacy enabled status.
877 // This is to ensure that any new usages of this module type do not rely on legacy
878 // behaviour.
879 defaultEnabledStatus := false
880 if anyScopesExplicitlyEnabled {
881 defaultEnabledStatus = scope.defaultEnabledStatus
882 } else {
883 defaultEnabledStatus = scope.legacyEnabledStatus(module)
884 }
885 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
886 if enabled {
887 enabledScopes[scope] = struct{}{}
888 generatedScopes = append(generatedScopes, scope)
889 }
890 }
891
892 // Now check to make sure that any scope that is extended by an enabled scope is also
893 // enabled.
894 for _, scope := range allApiScopes {
895 if _, ok := enabledScopes[scope]; ok {
896 extends := scope.extends
897 if extends != nil {
898 if _, ok := enabledScopes[extends]; !ok {
899 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
900 }
901 }
902 }
903 }
904
905 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000906}
907
Paul Duffine74ac732020-02-06 13:51:46 +0000908var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
909
Jiyong Parke3833882020-02-17 17:28:10 +0900910func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
911 if dt, ok := depTag.(dependencyTag); ok {
912 return dt == xmlPermissionsFileTag
913 }
914 return false
915}
916
Paul Duffin5df79302020-05-16 15:52:12 +0100917var implLibraryTag = dependencyTag{name: "impl-library"}
918
Inseob Kimc0907f12019-02-08 21:00:45 +0900919func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100920 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000921 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100922 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000923
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100924 // If the stubs source and API cannot be generated together then add an additional dependency on
925 // the API module.
926 if apiScope.createStubsSourceAndApiTogether {
927 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinc3091c82020-05-08 14:16:20 +0100928 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100929 } else {
930 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinc3091c82020-05-08 14:16:20 +0100931 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
932 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100933 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900934 }
935
Paul Duffindfa131e2020-05-15 20:37:11 +0100936 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +0100937 // Add dependency to the rule for generating the implementation library.
938 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
939
Paul Duffindfa131e2020-05-15 20:37:11 +0100940 if module.sharedLibrary() {
941 // Add dependency to the rule for generating the xml permissions file
942 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
943 }
Paul Duffine74ac732020-02-06 13:51:46 +0000944
Paul Duffindfa131e2020-05-15 20:37:11 +0100945 // Only add the deps for the library if it is actually going to be built.
946 module.Library.deps(ctx)
947 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900948}
949
Paul Duffin46dc45a2020-05-14 15:39:10 +0100950func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
951 paths, err := module.commonOutputFiles(tag)
952 if paths == nil && err == nil {
953 return module.Library.OutputFiles(tag)
954 } else {
955 return paths, err
956 }
957}
958
Inseob Kimc0907f12019-02-08 21:00:45 +0900959func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +0100960 // Only build an implementation library if required.
961 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +0000962 module.Library.GenerateAndroidBuildActions(ctx)
963 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900964
Sundong Ahn57368eb2018-07-06 11:20:23 +0900965 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000966 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900967 // the recorded paths will be returned depending on the link type of the caller.
968 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900969 tag := ctx.OtherModuleDependencyTag(to)
970
Paul Duffinc8782502020-04-29 20:45:27 +0100971 // Extract information from any of the scope specific dependencies.
972 if scopeTag, ok := tag.(scopeDependencyTag); ok {
973 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +0100974 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +0100975
976 // Extract information from the dependency. The exact information extracted
977 // is determined by the nature of the dependency which is determined by the tag.
978 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900979 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900980 })
981}
982
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900983func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +0100984 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +0000985 return nil
986 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900987 entriesList := module.Library.AndroidMkEntries()
988 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700989 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900990 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900991}
992
Jiyong Parkc678ad32018-04-10 13:07:10 +0900993// Module name of the runtime implementation library
Paul Duffin5df79302020-05-16 15:52:12 +0100994func (module *SdkLibrary) implLibraryModuleName() string {
995 return module.BaseModuleName() + ".impl"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900996}
997
Jiyong Parkc678ad32018-04-10 13:07:10 +0900998// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900999func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001000 return module.BaseModuleName() + sdkXmlFileSuffix
1001}
1002
Anton Hansson5fd5d242020-03-27 19:43:19 +00001003// The dist path of the stub artifacts
1004func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1005 if module.ModuleBase.Owner() != "" {
1006 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1007 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1008 return path.Join("apistubs", "core", apiScope.name)
1009 } else {
1010 return path.Join("apistubs", "android", apiScope.name)
1011 }
1012}
1013
Paul Duffin12ceb462019-12-24 20:31:31 +00001014// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001015func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001016 scopeProperties := module.scopeToProperties[apiScope]
1017 if scopeProperties.Sdk_version != nil {
1018 return proptools.String(scopeProperties.Sdk_version)
1019 }
1020
Paul Duffin12ceb462019-12-24 20:31:31 +00001021 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1022 if sdkDep.hasStandardLibs() {
1023 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001024 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001025 } else {
1026 // Otherwise, use no system module.
1027 return "none"
1028 }
1029}
1030
Paul Duffind1b3a922020-01-22 11:57:20 +00001031func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1032 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001033}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001034
Paul Duffind1b3a922020-01-22 11:57:20 +00001035func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1036 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001037}
1038
Paul Duffin5df79302020-05-16 15:52:12 +01001039// Creates the implementation java library
1040func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
1041 props := struct {
1042 Name *string
1043 Visibility []string
1044 }{
1045 Name: proptools.StringPtr(module.implLibraryModuleName()),
1046 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
1047 }
1048
1049 properties := []interface{}{
1050 &module.properties,
1051 &module.protoProperties,
1052 &module.deviceProperties,
1053 &module.dexpreoptProperties,
1054 &props,
1055 module.sdkComponentPropertiesForChildLibrary(),
1056 }
1057 mctx.CreateModule(LibraryFactory, properties...)
1058}
1059
Jiyong Parkc678ad32018-04-10 13:07:10 +09001060// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001061func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001062 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001063 Name *string
1064 Visibility []string
1065 Srcs []string
1066 Installable *bool
1067 Sdk_version *string
1068 System_modules *string
1069 Patch_module *string
1070 Libs []string
1071 Compile_dex *bool
1072 Java_version *string
1073 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001074 Pdk struct {
1075 Enabled *bool
1076 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001077 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001078 Openjdk9 struct {
1079 Srcs []string
1080 Javacflags []string
1081 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001082 Dist struct {
1083 Targets []string
1084 Dest *string
1085 Dir *string
1086 Tag *string
1087 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001088 }{}
1089
Paul Duffinc3091c82020-05-08 14:16:20 +01001090 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +01001091
1092 // If stubs_library_visibility is not set then the created module will use the
1093 // visibility of this module.
1094 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1095 props.Visibility = visibility
1096
Jiyong Parkc678ad32018-04-10 13:07:10 +09001097 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001098 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001099 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001100 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001101 props.System_modules = module.deviceProperties.System_modules
1102 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001103 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001104 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +09001105 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffina18abc22020-05-16 18:54:24 +01001106 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1107 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
1108 props.Java_version = module.properties.Java_version
1109 if module.deviceProperties.Compile_dex != nil {
1110 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001111 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001112
Anton Hansson5fd5d242020-03-27 19:43:19 +00001113 // Dist the class jar artifact for sdk builds.
1114 if !Bool(module.sdkLibraryProperties.No_dist) {
1115 props.Dist.Targets = []string{"sdk", "win_sdk"}
1116 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1117 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1118 props.Dist.Tag = proptools.StringPtr(".jar")
1119 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001120
Paul Duffin859fe962020-05-15 10:20:31 +01001121 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001122}
1123
Paul Duffin6d0886e2020-04-07 18:49:53 +01001124// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001125// files and also updates and checks the API specification files.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001126func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001127 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001128 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001129 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001130 Srcs []string
1131 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001132 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001133 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001134 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001135 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001136 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001137 Java_version *string
1138 Merge_annotations_dirs []string
1139 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001140 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001141 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001142 Current ApiToCheck
1143 Last_released ApiToCheck
1144 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001145
1146 Api_lint struct {
1147 Enabled *bool
1148 New_since *string
1149 Baseline_file *string
1150 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001151 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001152 Aidl struct {
1153 Include_dirs []string
1154 Local_include_dirs []string
1155 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001156 Dist struct {
1157 Targets []string
1158 Dest *string
1159 Dir *string
1160 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001161 }{}
1162
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001163 // The stubs source processing uses the same compile time classpath when extracting the
1164 // API from the implementation library as it does when compiling it. i.e. the same
1165 // * sdk version
1166 // * system_modules
1167 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001168
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001169 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +01001170
1171 // If stubs_source_visibility is not set then the created module will use the
1172 // visibility of this module.
1173 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1174 props.Visibility = visibility
1175
Paul Duffina18abc22020-05-16 18:54:24 +01001176 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1177 props.Sdk_version = module.deviceProperties.Sdk_version
1178 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001179 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001180 // A droiddoc module has only one Libs property and doesn't distinguish between
1181 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001182 props.Libs = module.properties.Libs
1183 props.Libs = append(props.Libs, module.properties.Static_libs...)
1184 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1185 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1186 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001187
Sundong Ahn054b19a2018-10-19 13:46:09 +09001188 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1189 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1190
Paul Duffin6d0886e2020-04-07 18:49:53 +01001191 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001192 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001193 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001194 }
1195 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001196 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001197 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1198 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001199 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001200 disabledWarnings := []string{
1201 "MissingPermission",
1202 "BroadcastBehavior",
1203 "HiddenSuperclass",
1204 "DeprecationMismatch",
1205 "UnavailableSymbol",
1206 "SdkConstant",
1207 "HiddenTypeParameter",
1208 "Todo",
1209 "Typo",
1210 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001211 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001212
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001213 if !createStubSources {
1214 // Stubs are not required.
1215 props.Generate_stubs = proptools.BoolPtr(false)
1216 }
1217
Paul Duffin1fb487d2020-04-07 18:50:10 +01001218 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001219 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001220 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001221 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001222
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001223 if createApi {
1224 // List of APIs identified from the provided source files are created. They are later
1225 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1226 // last-released (a.k.a numbered) list of API.
1227 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1228 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1229 apiDir := module.getApiDir()
1230 currentApiFileName = path.Join(apiDir, currentApiFileName)
1231 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001232
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001233 // check against the not-yet-release API
1234 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1235 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001236
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001237 if !apiScope.unstable {
1238 // check against the latest released API
1239 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1240 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1241 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1242 module.latestRemovedApiFilegroupName(apiScope))
1243 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001244
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001245 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1246 // Enable api lint.
1247 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1248 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001249
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001250 // If it exists then pass a lint-baseline.txt through to droidstubs.
1251 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1252 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1253 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1254 if err != nil {
1255 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1256 }
1257 if len(paths) == 1 {
1258 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1259 } else if len(paths) != 0 {
1260 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1261 }
Paul Duffin160fe412020-05-10 19:32:20 +01001262 }
1263 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001264
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001265 // Dist the api txt artifact for sdk builds.
1266 if !Bool(module.sdkLibraryProperties.No_dist) {
1267 props.Dist.Targets = []string{"sdk", "win_sdk"}
1268 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1269 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1270 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001271 }
1272
Colin Cross84dfc3d2019-09-25 11:33:01 -07001273 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001274}
1275
Jooyung Han5e9013b2020-03-10 06:23:13 +09001276func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1277 depTag := mctx.OtherModuleDependencyTag(dep)
1278 if depTag == xmlPermissionsFileTag {
1279 return true
1280 }
1281 return module.Library.DepIsInSameApex(mctx, dep)
1282}
1283
Jiyong Parkc678ad32018-04-10 13:07:10 +09001284// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001285func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001286 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001287 Name *string
1288 Lib_name *string
1289 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001290 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +09001291 Name: proptools.StringPtr(module.xmlFileName()),
1292 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1293 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001294 }
Jiyong Parke3833882020-02-17 17:28:10 +09001295
Jiyong Parke3833882020-02-17 17:28:10 +09001296 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001297}
1298
Paul Duffin50061512020-01-21 16:31:05 +00001299func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001300 var ver sdkVersion
1301 var kind sdkKind
1302 if s.usePrebuilt(ctx) {
1303 ver = s.version
1304 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001305 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001306 // We don't have prebuilt SDK for the specific sdkVersion.
1307 // Instead of breaking the build, fallback to use "system_current"
1308 ver = sdkVersionCurrent
1309 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001310 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001311
1312 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001313 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001314 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001315 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001316 if ctx.Config().AllowMissingDependencies() {
1317 return android.Paths{android.PathForSource(ctx, jar)}
1318 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001319 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001320 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001321 return nil
1322 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001323 return android.Paths{jarPath.Path()}
1324}
1325
Paul Duffin9b879592020-05-26 13:21:35 +01001326// Get the apex name for module, "" if it is for platform.
1327func getApexNameForModule(module android.Module) string {
1328 if apex, ok := module.(android.ApexModule); ok {
1329 return apex.ApexName()
1330 }
1331
1332 return ""
1333}
1334
1335// Check to see if the other module is within the same named APEX as this module.
1336//
1337// If either this or the other module are on the platform then this will return
1338// false.
1339func (module *SdkLibrary) withinSameApexAs(other android.Module) bool {
1340 name := module.ApexName()
1341 return name != "" && getApexNameForModule(other) == name
1342}
1343
Paul Duffinb05d4292020-05-20 12:19:10 +01001344func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001345
Paul Duffindaaa3322020-05-26 18:13:57 +01001346 // Only provide access to the implementation library if it is actually built.
1347 if module.requiresRuntimeImplementationLibrary() {
1348 // Check any special cases for java_sdk_library.
1349 //
1350 // Only allow access to the implementation library in the following condition:
1351 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001352 // * The referencing module is in the same apex as this.
1353 if sdkVersion.kind == sdkPrivate || module.withinSameApexAs(ctx.Module()) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001354 if headerJars {
1355 return module.HeaderJars()
1356 } else {
1357 return module.ImplementationJars()
1358 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001359 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001360 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001361
Paul Duffin23970f42020-05-20 14:20:02 +01001362 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001363}
1364
Sundong Ahn241cd372018-07-13 16:16:44 +09001365// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001366func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1367 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1368}
1369
1370// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001371func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001372 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001373}
1374
Sundong Ahn80a87b32019-05-13 15:02:50 +09001375func (module *SdkLibrary) SetNoDist() {
1376 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1377}
1378
Colin Cross571cccf2019-02-04 11:22:08 -08001379var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1380
Jiyong Park82484c02018-04-23 21:41:26 +09001381func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001382 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001383 return &[]string{}
1384 }).(*[]string)
1385}
1386
Paul Duffin749f98f2019-12-30 17:23:46 +00001387func (module *SdkLibrary) getApiDir() string {
1388 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1389}
1390
Jiyong Parkc678ad32018-04-10 13:07:10 +09001391// For a java_sdk_library module, create internal modules for stubs, docs,
1392// runtime libs and xml file. If requested, the stubs and docs are created twice
1393// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001394func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1395 // If the module has been disabled then don't create any child modules.
1396 if !module.Enabled() {
1397 return
1398 }
1399
Paul Duffina18abc22020-05-16 18:54:24 +01001400 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001401 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001402 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001403 }
1404
Paul Duffin37e0b772019-12-30 17:20:10 +00001405 // If this builds against standard libraries (i.e. is not part of the core libraries)
1406 // then assume it provides both system and test apis. Otherwise, assume it does not and
1407 // also assume it does not contribute to the dist build.
1408 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1409 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001410 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001411 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1412
Inseob Kim8098faa2019-03-18 10:19:51 +09001413 missing_current_api := false
1414
Paul Duffin3375e352020-04-28 10:44:03 +01001415 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001416
Paul Duffin749f98f2019-12-30 17:23:46 +00001417 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001418 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001419 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001420 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001421 p := android.ExistentPathForSource(mctx, path)
1422 if !p.Valid() {
1423 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1424 missing_current_api = true
1425 }
1426 }
1427 }
1428
1429 if missing_current_api {
1430 script := "build/soong/scripts/gen-java-current-api-files.sh"
1431 p := android.ExistentPathForSource(mctx, script)
1432
1433 if !p.Valid() {
1434 panic(fmt.Sprintf("script file %s doesn't exist", script))
1435 }
1436
1437 mctx.ModuleErrorf("One or more current api files are missing. "+
1438 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001439 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001440 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001441 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001442 return
1443 }
1444
Paul Duffin3375e352020-04-28 10:44:03 +01001445 for _, scope := range generatedScopes {
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001446 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinc3091c82020-05-08 14:16:20 +01001447 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001448
1449 // If the args needed to generate the stubs and API are the same then they
1450 // can be generated in a single invocation of metalava, otherwise they will
1451 // need separate invocations.
1452 if scope.createStubsSourceAndApiTogether {
1453 // Use the stubs source name for legacy reasons.
1454 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1455 } else {
1456 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1457
1458 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinc3091c82020-05-08 14:16:20 +01001459 apiName := module.apiModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001460 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1461 }
1462
Paul Duffind1b3a922020-01-22 11:57:20 +00001463 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001464 }
1465
Paul Duffindfa131e2020-05-15 20:37:11 +01001466 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001467 // Create child module to create an implementation library.
1468 //
1469 // This temporarily creates a second implementation library that can be explicitly
1470 // referenced.
1471 //
1472 // TODO(b/156618935) - update comment once only one implementation library is created.
1473 module.createImplLibrary(mctx)
1474
Paul Duffindfa131e2020-05-15 20:37:11 +01001475 // Only create an XML permissions file that declares the library as being usable
1476 // as a shared library if required.
1477 if module.sharedLibrary() {
1478 module.createXmlFile(mctx)
1479 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001480
1481 // record java_sdk_library modules so that they are exported to make
1482 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1483 javaSdkLibrariesLock.Lock()
1484 defer javaSdkLibrariesLock.Unlock()
1485 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1486 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001487}
1488
1489func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001490 module.AddProperties(
1491 &module.sdkLibraryProperties,
Paul Duffina18abc22020-05-16 18:54:24 +01001492 &module.properties,
1493 &module.dexpreoptProperties,
1494 &module.deviceProperties,
1495 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001496 )
1497
Paul Duffin859fe962020-05-15 10:20:31 +01001498 module.initSdkLibraryComponent(&module.ModuleBase)
1499
Paul Duffina18abc22020-05-16 18:54:24 +01001500 module.properties.Installable = proptools.BoolPtr(true)
1501 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001502}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001503
Paul Duffindfa131e2020-05-15 20:37:11 +01001504func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1505 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1506}
1507
Paul Duffin1b1e8062020-05-08 13:44:43 +01001508// Defines how to name the individual component modules the sdk library creates.
1509type sdkLibraryComponentNamingScheme interface {
1510 stubsLibraryModuleName(scope *apiScope, baseName string) string
1511
1512 stubsSourceModuleName(scope *apiScope, baseName string) string
1513
1514 apiModuleName(scope *apiScope, baseName string) string
1515}
1516
1517type defaultNamingScheme struct {
1518}
1519
1520func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1521 return scope.stubsLibraryModuleName(baseName)
1522}
1523
1524func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1525 return scope.stubsSourceModuleName(baseName)
1526}
1527
1528func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1529 return scope.apiModuleName(baseName)
1530}
1531
1532var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1533
Paul Duffin6c9c5fc2020-05-08 15:36:30 +01001534type frameworkModulesNamingScheme struct {
1535}
1536
1537func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1538 suffix := scope.name
1539 if scope == apiScopeModuleLib {
1540 suffix = "module_libs_"
1541 }
1542 return suffix
1543}
1544
1545func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1546 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1547}
1548
1549func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1550 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1551}
1552
1553func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1554 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1555}
1556
1557var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1558
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001559// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1560// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1561// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1562// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1563// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001564func SdkLibraryFactory() android.Module {
1565 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001566
1567 // Initialize information common between source and prebuilt.
1568 module.initCommon(&module.ModuleBase)
1569
Inseob Kimc0907f12019-02-08 21:00:45 +09001570 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001571 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001572 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001573
1574 // Initialize the map from scope to scope specific properties.
1575 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1576 for _, scope := range allApiScopes {
1577 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1578 }
1579 module.scopeToProperties = scopeToProperties
1580
Paul Duffin4911a892020-04-29 23:35:13 +01001581 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001582 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001583 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1584 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1585
Paul Duffin1b1e8062020-05-08 13:44:43 +01001586 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001587 // If no implementation is required then it cannot be used as a shared library
1588 // either.
1589 if !module.requiresRuntimeImplementationLibrary() {
1590 // If shared_library has been explicitly set to true then it is incompatible
1591 // with api_only: true.
1592 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1593 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1594 }
1595 // Set shared_library: false.
1596 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1597 }
1598
Paul Duffin1b1e8062020-05-08 13:44:43 +01001599 if module.initCommonAfterDefaultsApplied(ctx) {
1600 module.CreateInternalModules(ctx)
1601 }
1602 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001603 return module
1604}
Colin Cross79c7c262019-04-17 11:11:46 -07001605
1606//
1607// SDK library prebuilts
1608//
1609
Paul Duffin56d44902020-01-31 13:36:25 +00001610// Properties associated with each api scope.
1611type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001612 Jars []string `android:"path"`
1613
1614 Sdk_version *string
1615
Colin Cross79c7c262019-04-17 11:11:46 -07001616 // List of shared java libs that this module has dependencies to
1617 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001618
Paul Duffinc8782502020-04-29 20:45:27 +01001619 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001620 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001621
1622 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001623 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001624
1625 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001626 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001627}
1628
Paul Duffin56d44902020-01-31 13:36:25 +00001629type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001630 // List of shared java libs, common to all scopes, that this module has
1631 // dependencies to
1632 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001633}
1634
Colin Cross79c7c262019-04-17 11:11:46 -07001635type sdkLibraryImport struct {
1636 android.ModuleBase
1637 android.DefaultableModuleBase
1638 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001639 android.ApexModuleBase
1640 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001641
1642 properties sdkLibraryImportProperties
1643
Paul Duffin46a26a82020-04-07 19:27:04 +01001644 // Map from api scope to the scope specific property structure.
1645 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1646
Paul Duffin56d44902020-01-31 13:36:25 +00001647 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001648}
1649
1650var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1651
Paul Duffin46a26a82020-04-07 19:27:04 +01001652// The type of a structure that contains a field of type sdkLibraryScopeProperties
1653// for each apiscope in allApiScopes, e.g. something like:
1654// struct {
1655// Public sdkLibraryScopeProperties
1656// System sdkLibraryScopeProperties
1657// ...
1658// }
1659var allScopeStructType = createAllScopePropertiesStructType()
1660
1661// Dynamically create a structure type for each apiscope in allApiScopes.
1662func createAllScopePropertiesStructType() reflect.Type {
1663 var fields []reflect.StructField
1664 for _, apiScope := range allApiScopes {
1665 field := reflect.StructField{
1666 Name: apiScope.fieldName,
1667 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1668 }
1669 fields = append(fields, field)
1670 }
1671
1672 return reflect.StructOf(fields)
1673}
1674
1675// Create an instance of the scope specific structure type and return a map
1676// from apiscope to a pointer to each scope specific field.
1677func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1678 allScopePropertiesPtr := reflect.New(allScopeStructType)
1679 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1680 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1681
1682 for _, apiScope := range allApiScopes {
1683 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1684 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1685 }
1686
1687 return allScopePropertiesPtr.Interface(), scopeProperties
1688}
1689
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001690// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001691func sdkLibraryImportFactory() android.Module {
1692 module := &sdkLibraryImport{}
1693
Paul Duffin46a26a82020-04-07 19:27:04 +01001694 allScopeProperties, scopeToProperties := createPropertiesInstance()
1695 module.scopeProperties = scopeToProperties
1696 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001697
Paul Duffinc3091c82020-05-08 14:16:20 +01001698 // Initialize information common between source and prebuilt.
1699 module.initCommon(&module.ModuleBase)
1700
Paul Duffin0bdcb272020-02-06 15:24:57 +00001701 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001702 android.InitApexModule(module)
1703 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001704 InitJavaModule(module, android.HostAndDeviceSupported)
1705
Paul Duffin1b1e8062020-05-08 13:44:43 +01001706 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1707 if module.initCommonAfterDefaultsApplied(mctx) {
1708 module.createInternalModules(mctx)
1709 }
1710 })
Colin Cross79c7c262019-04-17 11:11:46 -07001711 return module
1712}
1713
1714func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1715 return &module.prebuilt
1716}
1717
1718func (module *sdkLibraryImport) Name() string {
1719 return module.prebuilt.Name(module.ModuleBase.Name())
1720}
1721
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001722func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001723
Paul Duffin50061512020-01-21 16:31:05 +00001724 // If the build is configured to use prebuilts then force this to be preferred.
1725 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1726 module.prebuilt.ForcePrefer()
1727 }
1728
Paul Duffin46a26a82020-04-07 19:27:04 +01001729 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001730 if len(scopeProperties.Jars) == 0 {
1731 continue
1732 }
1733
Paul Duffinbbb546b2020-04-09 00:07:11 +01001734 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001735
Paul Duffin0f8faff2020-05-20 16:18:00 +01001736 if len(scopeProperties.Stub_srcs) > 0 {
1737 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1738 }
Paul Duffin56d44902020-01-31 13:36:25 +00001739 }
Colin Cross79c7c262019-04-17 11:11:46 -07001740
1741 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1742 javaSdkLibrariesLock.Lock()
1743 defer javaSdkLibrariesLock.Unlock()
1744 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1745}
1746
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001747func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001748 // Creates a java import for the jar with ".stubs" suffix
1749 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001750 Name *string
1751 Sdk_version *string
1752 Libs []string
1753 Jars []string
1754 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001755 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001756 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001757 props.Sdk_version = scopeProperties.Sdk_version
1758 // Prepend any of the libs from the legacy public properties to the libs for each of the
1759 // scopes to avoid having to duplicate them in each scope.
1760 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1761 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001762
Paul Duffin38b57852020-05-13 16:08:09 +01001763 // The imports are preferred if the java_sdk_library_import is preferred.
1764 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001765
1766 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001767}
1768
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001769func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001770 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001771 Name *string
1772 Srcs []string
1773 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001774 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001775 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001776 props.Srcs = scopeProperties.Stub_srcs
1777 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001778
1779 // The stubs source is preferred if the java_sdk_library_import is preferred.
1780 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001781}
1782
Colin Cross79c7c262019-04-17 11:11:46 -07001783func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001784 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001785 if len(scopeProperties.Jars) == 0 {
1786 continue
1787 }
1788
1789 // Add dependencies to the prebuilt stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001790 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001791
1792 if len(scopeProperties.Stub_srcs) > 0 {
1793 // Add dependencies to the prebuilt stubs source library
1794 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1795 }
Paul Duffin56d44902020-01-31 13:36:25 +00001796 }
Colin Cross79c7c262019-04-17 11:11:46 -07001797}
1798
Paul Duffin46dc45a2020-05-14 15:39:10 +01001799func (module *sdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
1800 return module.commonOutputFiles(tag)
1801}
1802
Colin Cross79c7c262019-04-17 11:11:46 -07001803func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin0f8faff2020-05-20 16:18:00 +01001804 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001805 ctx.VisitDirectDeps(func(to android.Module) {
1806 tag := ctx.OtherModuleDependencyTag(to)
1807
Paul Duffin0f8faff2020-05-20 16:18:00 +01001808 // Extract information from any of the scope specific dependencies.
1809 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1810 apiScope := scopeTag.apiScope
1811 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1812
1813 // Extract information from the dependency. The exact information extracted
1814 // is determined by the nature of the dependency which is determined by the tag.
1815 scopeTag.extractDepInfo(ctx, to, scopePaths)
Colin Cross79c7c262019-04-17 11:11:46 -07001816 }
1817 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001818
1819 // Populate the scope paths with information from the properties.
1820 for apiScope, scopeProperties := range module.scopeProperties {
1821 if len(scopeProperties.Jars) == 0 {
1822 continue
1823 }
1824
1825 paths := module.getScopePathsCreateIfNeeded(apiScope)
1826 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1827 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1828 }
Colin Cross79c7c262019-04-17 11:11:46 -07001829}
1830
Paul Duffinb05d4292020-05-20 12:19:10 +01001831func (module *sdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin23970f42020-05-20 14:20:02 +01001832 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001833}
1834
Colin Cross79c7c262019-04-17 11:11:46 -07001835// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001836func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001837 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001838 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001839}
1840
1841// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001842func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001843 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001844 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001845}
Jiyong Parke3833882020-02-17 17:28:10 +09001846
1847//
1848// java_sdk_library_xml
1849//
1850type sdkLibraryXml struct {
1851 android.ModuleBase
1852 android.DefaultableModuleBase
1853 android.ApexModuleBase
1854
1855 properties sdkLibraryXmlProperties
1856
1857 outputFilePath android.OutputPath
1858 installDirPath android.InstallPath
1859}
1860
1861type sdkLibraryXmlProperties struct {
1862 // canonical name of the lib
1863 Lib_name *string
1864}
1865
1866// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1867// Not to be used directly by users. java_sdk_library internally uses this.
1868func sdkLibraryXmlFactory() android.Module {
1869 module := &sdkLibraryXml{}
1870
1871 module.AddProperties(&module.properties)
1872
1873 android.InitApexModule(module)
1874 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1875
1876 return module
1877}
1878
1879// from android.PrebuiltEtcModule
1880func (module *sdkLibraryXml) SubDir() string {
1881 return "permissions"
1882}
1883
1884// from android.PrebuiltEtcModule
1885func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1886 return module.outputFilePath
1887}
1888
1889// from android.ApexModule
1890func (module *sdkLibraryXml) AvailableFor(what string) bool {
1891 return true
1892}
1893
1894func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1895 // do nothing
1896}
1897
1898// File path to the runtime implementation library
1899func (module *sdkLibraryXml) implPath() string {
1900 implName := proptools.String(module.properties.Lib_name)
1901 if apexName := module.ApexName(); apexName != "" {
1902 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1903 // In most cases, this works fine. But when apex_name is set or override_apex is used
1904 // this can be wrong.
1905 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1906 }
1907 partition := "system"
1908 if module.SocSpecific() {
1909 partition = "vendor"
1910 } else if module.DeviceSpecific() {
1911 partition = "odm"
1912 } else if module.ProductSpecific() {
1913 partition = "product"
1914 } else if module.SystemExtSpecific() {
1915 partition = "system_ext"
1916 }
1917 return "/" + partition + "/framework/" + implName + ".jar"
1918}
1919
1920func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1921 libName := proptools.String(module.properties.Lib_name)
1922 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1923
1924 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1925 rule := android.NewRuleBuilder()
1926 rule.Command().
1927 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1928 Output(module.outputFilePath)
1929
1930 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1931
1932 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1933}
1934
1935func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1936 if !module.IsForPlatform() {
1937 return []android.AndroidMkEntries{android.AndroidMkEntries{
1938 Disabled: true,
1939 }}
1940 }
1941
1942 return []android.AndroidMkEntries{android.AndroidMkEntries{
1943 Class: "ETC",
1944 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1945 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1946 func(entries *android.AndroidMkEntries) {
1947 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1948 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1949 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1950 },
1951 },
1952 }}
1953}
Paul Duffindd46f712020-02-10 13:37:10 +00001954
1955type sdkLibrarySdkMemberType struct {
1956 android.SdkMemberTypeBase
1957}
1958
1959func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1960 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1961}
1962
1963func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1964 _, ok := module.(*SdkLibrary)
1965 return ok
1966}
1967
1968func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1969 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1970}
1971
1972func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1973 return &sdkLibrarySdkMemberProperties{}
1974}
1975
1976type sdkLibrarySdkMemberProperties struct {
1977 android.SdkMemberPropertiesBase
1978
1979 // Scope to per scope properties.
1980 Scopes map[*apiScope]scopeProperties
1981
1982 // Additional libraries that the exported stubs libraries depend upon.
1983 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001984
1985 // The Java stubs source files.
1986 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01001987
1988 // The naming scheme.
1989 Naming_scheme *string
Paul Duffindd46f712020-02-10 13:37:10 +00001990}
1991
1992type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001993 Jars android.Paths
1994 StubsSrcJar android.Path
1995 CurrentApiFile android.Path
1996 RemovedApiFile android.Path
1997 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001998}
1999
2000func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2001 sdk := variant.(*SdkLibrary)
2002
2003 s.Scopes = make(map[*apiScope]scopeProperties)
2004 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002005 paths := sdk.findScopePaths(apiScope)
2006 if paths == nil {
2007 continue
2008 }
2009
Paul Duffindd46f712020-02-10 13:37:10 +00002010 jars := paths.stubsImplPath
2011 if len(jars) > 0 {
2012 properties := scopeProperties{}
2013 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002014 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002015 properties.StubsSrcJar = paths.stubsSrcJar.Path()
2016 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2017 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffindd46f712020-02-10 13:37:10 +00002018 s.Scopes[apiScope] = properties
2019 }
2020 }
2021
2022 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01002023 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffindd46f712020-02-10 13:37:10 +00002024}
2025
2026func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002027 if s.Naming_scheme != nil {
2028 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2029 }
2030
Paul Duffindd46f712020-02-10 13:37:10 +00002031 for _, apiScope := range allApiScopes {
2032 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002033 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002034
Paul Duffin3d1248c2020-04-09 00:10:17 +01002035 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2036
Paul Duffindd46f712020-02-10 13:37:10 +00002037 var jars []string
2038 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002039 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002040 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2041 jars = append(jars, dest)
2042 }
2043 scopeSet.AddProperty("jars", jars)
2044
Paul Duffin3d1248c2020-04-09 00:10:17 +01002045 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2046 // the source files are also unpacked.
2047 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2048 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2049 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2050
Paul Duffin1fd005d2020-04-09 01:08:11 +01002051 if properties.CurrentApiFile != nil {
2052 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2053 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2054 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2055 }
2056
2057 if properties.RemovedApiFile != nil {
2058 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
2059 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
2060 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2061 }
2062
Paul Duffindd46f712020-02-10 13:37:10 +00002063 if properties.SdkVersion != "" {
2064 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2065 }
2066 }
2067 }
2068
2069 if len(s.Libs) > 0 {
2070 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2071 }
2072}