blob: de30635b410032a76b8bb733e51cd3e8e26cf26b [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin6a2bd112020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46fdda82020-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 Duffin6a2bd112020-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 Duffin1c094a02020-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 Duffin5fb82132020-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 Duffin51a2bee2020-05-05 14:40:52 +010078 // The api scope that this scope extends.
79 extends *apiScope
80
Paul Duffin3a254982020-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 Duffin6a2bd112020-04-07 19:27:04 +010093 // The name of the field in the dynamically created structure.
94 fieldName string
95
Paul Duffin0f270632020-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 Duffina377e4c2020-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 Duffin5fb82132020-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 Duffin3c7c3472020-04-07 18:50:10 +0100122
123 // Extra arguments to pass to droidstubs for this scope.
124 droidstubsArgs []string
Anton Hansson5ff28e52020-05-02 11:19:36 +0100125
Paul Duffina377e4c2020-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 Hansson5ff28e52020-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 Duffin5fb82132020-04-29 20:45:27 +0100148 name := scope.name
Paul Duffin46fdda82020-05-14 15:39:10 +0100149 scopeByName[name] = scope
150 allScopeNames = append(allScopeNames, name)
Paul Duffin0f270632020-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 Duffin5fb82132020-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 Duffina377e4c2020-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 Duffin5fb82132020-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 Duffina377e4c2020-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 Duffinb74ee3e2020-05-08 14:16:20 +0100196func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100197 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000198}
199
Paul Duffin5fb82132020-04-29 20:45:27 +0100200func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100201 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000202}
203
Paul Duffina377e4c2020-04-29 13:30:54 +0100204func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100205 return baseName + ".api" + scope.moduleSuffix
Paul Duffina377e4c2020-04-29 13:30:54 +0100206}
207
Paul Duffin3a254982020-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 Duffin46fdda82020-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 Duffin3a254982020-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 Duffin3a254982020-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 Hanssone366fff2020-04-28 16:47:41 +0100246 apiFilePrefix: "system-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100247 moduleSuffix: ".system",
Anton Hanssone366fff2020-04-28 16:47:41 +0100248 sdkVersion: "system_current",
Paul Duffin991f2622020-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 Duffin3a254982020-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 Hanssone366fff2020-04-28 16:47:41 +0100258 apiFilePrefix: "test-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100259 moduleSuffix: ".test",
Anton Hanssone366fff2020-04-28 16:47:41 +0100260 sdkVersion: "test_current",
261 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100262 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000263 })
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100264 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin0f270632020-05-13 19:19:49 +0100265 name: "module-lib",
Paul Duffin6d7f0a72020-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 Duffin6d7f0a72020-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 Duffin61871622020-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 Duffin3a254982020-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 Duffin080f5ee2020-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 Duffin3a254982020-04-28 10:44:03 +0100348}
349
Jiyong Parkc678ad32018-04-10 13:07:10 +0900350type sdkLibraryProperties struct {
Paul Duffin344c4ee2020-04-29 23:35:13 +0100351 // Visibility for stubs library modules. If not specified then defaults to the
352 // visibility property.
353 Stubs_library_visibility []string
354
355 // Visibility for stubs source modules. If not specified then defaults to the
356 // visibility property.
357 Stubs_source_visibility []string
358
Sundong Ahnf043cf62018-06-25 16:04:37 +0900359 // List of Java libraries that will be in the classpath when building stubs
360 Stub_only_libs []string `android:"arch_variant"`
361
Paul Duffin7a586d32019-12-30 17:09:34 +0000362 // list of package names that will be documented and publicized as API.
363 // This allows the API to be restricted to a subset of the source files provided.
364 // If this is unspecified then all the source files will be treated as being part
365 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900366 Api_packages []string
367
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900368 // list of package names that must be hidden from the API
369 Hidden_api_packages []string
370
Paul Duffin749f98f2019-12-30 17:23:46 +0000371 // the relative path to the directory containing the api specification files.
372 // Defaults to "api".
373 Api_dir *string
374
Paul Duffin43db9be2019-12-30 17:35:49 +0000375 // If set to true there is no runtime library.
376 Api_only *bool
377
Paul Duffin11512472019-02-11 15:55:17 +0000378 // local files that are used within user customized droiddoc options.
379 Droiddoc_option_files []string
380
381 // additional droiddoc options
382 // Available variables for substitution:
383 //
384 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900385 Droiddoc_options []string
386
Sundong Ahn054b19a2018-10-19 13:46:09 +0900387 // a list of top-level directories containing files to merge qualifier annotations
388 // (i.e. those intended to be included in the stubs written) from.
389 Merge_annotations_dirs []string
390
391 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
392 Merge_inclusion_annotations_dirs []string
393
394 // If set to true, the path of dist files is apistubs/core. Defaults to false.
395 Core_lib *bool
396
Sundong Ahn80a87b32019-05-13 15:02:50 +0900397 // don't create dist rules.
398 No_dist *bool `blueprint:"mutated"`
399
Paul Duffin3a254982020-04-28 10:44:03 +0100400 // indicates whether system and test apis should be generated.
401 Generate_system_and_test_apis bool `blueprint:"mutated"`
402
403 // The properties specific to the public api scope
404 //
405 // Unless explicitly specified by using public.enabled the public api scope is
406 // enabled by default in both legacy and non-legacy mode.
407 Public ApiScopeProperties
408
409 // The properties specific to the system api scope
410 //
411 // In legacy mode the system api scope is enabled by default when sdk_version
412 // is set to something other than "none".
413 //
414 // In non-legacy mode the system api scope is disabled by default.
415 System ApiScopeProperties
416
417 // The properties specific to the test api scope
418 //
419 // In legacy mode the test api scope is enabled by default when sdk_version
420 // is set to something other than "none".
421 //
422 // In non-legacy mode the test api scope is disabled by default.
423 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000424
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100425 // The properties specific to the module_lib api scope
426 //
427 // Unless explicitly specified by using test.enabled the module_lib api scope is
428 // disabled by default.
429 Module_lib ApiScopeProperties
430
Paul Duffin8986cc92020-05-10 19:32:20 +0100431 // Properties related to api linting.
432 Api_lint struct {
433 // Enable api linting.
434 Enabled *bool
435 }
436
Jiyong Parkc678ad32018-04-10 13:07:10 +0900437 // TODO: determines whether to create HTML doc or not
438 //Html_doc *bool
439}
440
Paul Duffin533f9c72020-05-20 16:18:00 +0100441// Paths to outputs from java_sdk_library and java_sdk_library_import.
442//
443// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
444// OptionalPaths are always set by java_sdk_library but may not be set by
445// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000446type scopePaths struct {
Paul Duffin533f9c72020-05-20 16:18:00 +0100447 // The path (represented as Paths for convenience when returning) to the stubs header jar.
448 //
449 // That is the jar that is created by turbine.
450 stubsHeaderPath android.Paths
451
452 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
453 //
454 // This is not the implementation jar, it still only contains stubs.
455 stubsImplPath android.Paths
456
457 // The API specification file, e.g. system_current.txt.
458 currentApiFilePath android.OptionalPath
459
460 // The specification of API elements removed since the last release.
461 removedApiFilePath android.OptionalPath
462
463 // The stubs source jar.
464 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000465}
466
Paul Duffin5fb82132020-04-29 20:45:27 +0100467func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
468 if lib, ok := dep.(Dependency); ok {
469 paths.stubsHeaderPath = lib.HeaderJars()
470 paths.stubsImplPath = lib.ImplementationJars()
471 return nil
472 } else {
473 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
474 }
475}
476
Paul Duffina377e4c2020-04-29 13:30:54 +0100477func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
478 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
479 action(apiStubsProvider)
Paul Duffin5fb82132020-04-29 20:45:27 +0100480 return nil
481 } else {
482 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
483 }
484}
485
Paul Duffin533f9c72020-05-20 16:18:00 +0100486func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
487 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
488 action(apiStubsProvider)
489 return nil
490 } else {
491 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
492 }
493}
494
Paul Duffina377e4c2020-04-29 13:30:54 +0100495func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin533f9c72020-05-20 16:18:00 +0100496 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
497 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffina377e4c2020-04-29 13:30:54 +0100498}
499
500func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
501 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
502 paths.extractApiInfoFromApiStubsProvider(provider)
503 })
504}
505
Paul Duffin533f9c72020-05-20 16:18:00 +0100506func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
507 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffina377e4c2020-04-29 13:30:54 +0100508}
509
510func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin533f9c72020-05-20 16:18:00 +0100511 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffina377e4c2020-04-29 13:30:54 +0100512 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
513 })
514}
515
516func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
517 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
518 paths.extractApiInfoFromApiStubsProvider(provider)
519 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
520 })
521}
522
523type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1a724e62020-05-08 13:44:43 +0100524 // The naming scheme to use for the components that this module creates.
525 //
Paul Duffindef8a892020-05-08 15:36:30 +0100526 // If not specified then it defaults to "default". The other allowable value is
527 // "framework-modules" which matches the scheme currently used by framework modules
528 // for the equivalent components represented as separate Soong modules.
Paul Duffin1a724e62020-05-08 13:44:43 +0100529 //
530 // This is a temporary mechanism to simplify conversion from separate modules for each
531 // component that follow a different naming pattern to the default one.
532 //
533 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffina377e4c2020-04-29 13:30:54 +0100534 Naming_scheme *string
535}
536
Paul Duffin56d44902020-01-31 13:36:25 +0000537// Common code between sdk library and sdk library import
538type commonToSdkLibraryAndImport struct {
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100539 moduleBase *android.ModuleBase
540
Paul Duffin56d44902020-01-31 13:36:25 +0000541 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100542
543 namingScheme sdkLibraryComponentNamingScheme
544
545 commonProperties commonToSdkLibraryAndImportProperties
Paul Duffin64e61992020-05-15 10:20:31 +0100546
547 // Functionality related to this being used as a component of a java_sdk_library.
548 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000549}
550
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100551func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
552 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100553
554 moduleBase.AddProperties(&c.commonProperties)
Paul Duffin64e61992020-05-15 10:20:31 +0100555
556 // Initialize this as an sdk library component.
557 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1a724e62020-05-08 13:44:43 +0100558}
559
560func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
561 schemeProperty := proptools.StringDefault(c.commonProperties.Naming_scheme, "default")
562 switch schemeProperty {
563 case "default":
564 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100565 case "framework-modules":
566 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100567 default:
568 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
569 return false
570 }
571
Paul Duffin64e61992020-05-15 10:20:31 +0100572 // Use the name specified in the module definition as the owner.
573 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
574
Paul Duffin1a724e62020-05-08 13:44:43 +0100575 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100576}
577
578// Name of the java_library module that compiles the stubs source.
579func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100580 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100581}
582
583// Name of the droidstubs module that generates the stubs source and may also
584// generate/check the API.
585func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100586 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100587}
588
589// Name of the droidstubs module that generates/checks the API. Only used if it
590// requires different arts to the stubs source generating module.
591func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100592 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100593}
594
Paul Duffin46fdda82020-05-14 15:39:10 +0100595// The component names for different outputs of the java_sdk_library.
596//
597// They are similar to the names used for the child modules it creates
598const (
599 stubsSourceComponentName = "stubs.source"
600
601 apiTxtComponentName = "api.txt"
602
603 removedApiTxtComponentName = "removed-api.txt"
604)
605
606// A regular expression to match tags that reference a specific stubs component.
607//
608// It will only match if given a valid scope and a valid component. It is verfy strict
609// to ensure it does not accidentally match a similar looking tag that should be processed
610// by the embedded Library.
611var tagSplitter = func() *regexp.Regexp {
612 // Given a list of literal string items returns a regular expression that will
613 // match any one of the items.
614 choice := func(items ...string) string {
615 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
616 }
617
618 // Regular expression to match one of the scopes.
619 scopesRegexp := choice(allScopeNames...)
620
621 // Regular expression to match one of the components.
622 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
623
624 // Regular expression to match any combination of one scope and one component.
625 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
626}()
627
628// For OutputFileProducer interface
629//
630// .<scope>.stubs.source
631// .<scope>.api.txt
632// .<scope>.removed-api.txt
633func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
634 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
635 scopeName := groups[1]
636 component := groups[2]
637
638 if scope, ok := scopeByName[scopeName]; ok {
639 paths := c.findScopePaths(scope)
640 if paths == nil {
641 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
642 }
643
644 switch component {
645 case stubsSourceComponentName:
646 if paths.stubsSrcJar.Valid() {
647 return android.Paths{paths.stubsSrcJar.Path()}, nil
648 }
649
650 case apiTxtComponentName:
651 if paths.currentApiFilePath.Valid() {
652 return android.Paths{paths.currentApiFilePath.Path()}, nil
653 }
654
655 case removedApiTxtComponentName:
656 if paths.removedApiFilePath.Valid() {
657 return android.Paths{paths.removedApiFilePath.Path()}, nil
658 }
659 }
660
661 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
662 } else {
663 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
664 }
665
666 } else {
667 return nil, nil
668 }
669}
670
Paul Duffin5ae30792020-05-20 11:52:25 +0100671func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000672 if c.scopePaths == nil {
673 c.scopePaths = make(map[*apiScope]*scopePaths)
674 }
675 paths := c.scopePaths[scope]
676 if paths == nil {
677 paths = &scopePaths{}
678 c.scopePaths[scope] = paths
679 }
680
681 return paths
682}
683
Paul Duffin5ae30792020-05-20 11:52:25 +0100684func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
685 if c.scopePaths == nil {
686 return nil
687 }
688
689 return c.scopePaths[scope]
690}
691
692// If this does not support the requested api scope then find the closest available
693// scope it does support. Returns nil if no such scope is available.
694func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
695 for s := scope; s != nil; s = s.extends {
696 if paths := c.findScopePaths(s); paths != nil {
697 return paths
698 }
699 }
700
701 // This should never happen outside tests as public should be the base scope for every
702 // scope and is enabled by default.
703 return nil
704}
705
Paul Duffina3fb67d2020-05-20 14:20:02 +0100706func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-05-20 12:19:10 +0100707
708 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
709 if sdkVersion.version.isNumbered() {
710 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
711 }
712
713 var apiScope *apiScope
714 switch sdkVersion.kind {
715 case sdkSystem:
716 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100717 case sdkModule:
718 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100719 case sdkTest:
720 apiScope = apiScopeTest
721 default:
722 apiScope = apiScopePublic
723 }
724
Paul Duffin5ae30792020-05-20 11:52:25 +0100725 paths := c.findClosestScopePath(apiScope)
726 if paths == nil {
727 var scopes []string
728 for _, s := range allApiScopes {
729 if c.findScopePaths(s) != nil {
730 scopes = append(scopes, s.name)
731 }
732 }
733 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
734 return nil
735 }
736
Paul Duffina3fb67d2020-05-20 14:20:02 +0100737 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100738}
739
Paul Duffin64e61992020-05-15 10:20:31 +0100740func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
741 componentProps := &struct {
742 SdkLibraryToImplicitlyTrack *string
743 }{
744 // Mark the stubs library as being components of this java_sdk_library so that
745 // any app that includes code which depends (directly or indirectly) on the stubs
746 // library will have the appropriate <uses-library> invocation inserted into its
747 // manifest if necessary.
748 SdkLibraryToImplicitlyTrack: proptools.StringPtr(c.moduleBase.BaseModuleName()),
749 }
750
751 return componentProps
752}
753
754// Properties related to the use of a module as an component of a java_sdk_library.
755type SdkLibraryComponentProperties struct {
756
757 // The name of the java_sdk_library/_import to add to a <uses-library> entry
758 // in the AndroidManifest.xml of any Android app that includes code that references
759 // this module. If not set then no java_sdk_library/_import is tracked.
760 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
761}
762
763// Structure to be embedded in a module struct that needs to support the
764// SdkLibraryComponentDependency interface.
765type EmbeddableSdkLibraryComponent struct {
766 sdkLibraryComponentProperties SdkLibraryComponentProperties
767}
768
769func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
770 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
771}
772
773// to satisfy SdkLibraryComponentDependency
774func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
775 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
776 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
777 }
778 return nil
779}
780
781// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
782// (including the java_sdk_library) itself.
783type SdkLibraryComponentDependency interface {
784 // The optional name of the sdk library that should be implicitly added to the
785 // AndroidManifest of an app that contains code which references the sdk library.
786 //
787 // Returns an array containing 0 or 1 items rather than a *string to make it easier
788 // to append this to the list of exported sdk libraries.
789 OptionalImplicitSdkLibrary() []string
790}
791
792// Make sure that all the module types that are components of java_sdk_library/_import
793// and which can be referenced (directly or indirectly) from an android app implement
794// the SdkLibraryComponentDependency interface.
795var _ SdkLibraryComponentDependency = (*Library)(nil)
796var _ SdkLibraryComponentDependency = (*Import)(nil)
797var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
798var _ SdkLibraryComponentDependency = (*sdkLibraryImport)(nil)
799
800// Provides access to sdk_version related header and implentation jars.
801type SdkLibraryDependency interface {
802 SdkLibraryComponentDependency
803
804 // Get the header jars appropriate for the supplied sdk_version.
805 //
806 // These are turbine generated jars so they only change if the externals of the
807 // class changes but it does not contain and implementation or JavaDoc.
808 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
809
810 // Get the implementation jars appropriate for the supplied sdk version.
811 //
812 // These are either the implementation jar for the whole sdk library or the implementation
813 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
814 // they are identical to the corresponding header jars.
815 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
816}
817
Inseob Kimc0907f12019-02-08 21:00:45 +0900818type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900819 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900820
Sundong Ahn054b19a2018-10-19 13:46:09 +0900821 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900822
Paul Duffin3a254982020-04-28 10:44:03 +0100823 // Map from api scope to the scope specific property structure.
824 scopeToProperties map[*apiScope]*ApiScopeProperties
825
Paul Duffin56d44902020-01-31 13:36:25 +0000826 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900827}
828
Inseob Kimc0907f12019-02-08 21:00:45 +0900829var _ Dependency = (*SdkLibrary)(nil)
830var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800831
Paul Duffin3a254982020-04-28 10:44:03 +0100832func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
833 return module.sdkLibraryProperties.Generate_system_and_test_apis
834}
835
836func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
837 // Check to see if any scopes have been explicitly enabled. If any have then all
838 // must be.
839 anyScopesExplicitlyEnabled := false
840 for _, scope := range allApiScopes {
841 scopeProperties := module.scopeToProperties[scope]
842 if scopeProperties.Enabled != nil {
843 anyScopesExplicitlyEnabled = true
844 break
845 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000846 }
Paul Duffin3a254982020-04-28 10:44:03 +0100847
848 var generatedScopes apiScopes
849 enabledScopes := make(map[*apiScope]struct{})
850 for _, scope := range allApiScopes {
851 scopeProperties := module.scopeToProperties[scope]
852 // If any scopes are explicitly enabled then ignore the legacy enabled status.
853 // This is to ensure that any new usages of this module type do not rely on legacy
854 // behaviour.
855 defaultEnabledStatus := false
856 if anyScopesExplicitlyEnabled {
857 defaultEnabledStatus = scope.defaultEnabledStatus
858 } else {
859 defaultEnabledStatus = scope.legacyEnabledStatus(module)
860 }
861 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
862 if enabled {
863 enabledScopes[scope] = struct{}{}
864 generatedScopes = append(generatedScopes, scope)
865 }
866 }
867
868 // Now check to make sure that any scope that is extended by an enabled scope is also
869 // enabled.
870 for _, scope := range allApiScopes {
871 if _, ok := enabledScopes[scope]; ok {
872 extends := scope.extends
873 if extends != nil {
874 if _, ok := enabledScopes[extends]; !ok {
875 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
876 }
877 }
878 }
879 }
880
881 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000882}
883
Paul Duffine74ac732020-02-06 13:51:46 +0000884var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
885
Jiyong Parke3833882020-02-17 17:28:10 +0900886func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
887 if dt, ok := depTag.(dependencyTag); ok {
888 return dt == xmlPermissionsFileTag
889 }
890 return false
891}
892
Inseob Kimc0907f12019-02-08 21:00:45 +0900893func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100894 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000895 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100896 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000897
Paul Duffina377e4c2020-04-29 13:30:54 +0100898 // If the stubs source and API cannot be generated together then add an additional dependency on
899 // the API module.
900 if apiScope.createStubsSourceAndApiTogether {
901 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100902 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100903 } else {
904 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100905 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
906 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100907 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900908 }
909
Paul Duffine74ac732020-02-06 13:51:46 +0000910 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
911 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900912 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000913 }
914
Sundong Ahn054b19a2018-10-19 13:46:09 +0900915 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900916}
917
Paul Duffin46fdda82020-05-14 15:39:10 +0100918func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
919 paths, err := module.commonOutputFiles(tag)
920 if paths == nil && err == nil {
921 return module.Library.OutputFiles(tag)
922 } else {
923 return paths, err
924 }
925}
926
Inseob Kimc0907f12019-02-08 21:00:45 +0900927func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000928 // Don't build an implementation library if this is api only.
929 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
930 module.Library.GenerateAndroidBuildActions(ctx)
931 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900932
Sundong Ahn57368eb2018-07-06 11:20:23 +0900933 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000934 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900935 // the recorded paths will be returned depending on the link type of the caller.
936 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900937 tag := ctx.OtherModuleDependencyTag(to)
938
Paul Duffin5fb82132020-04-29 20:45:27 +0100939 // Extract information from any of the scope specific dependencies.
940 if scopeTag, ok := tag.(scopeDependencyTag); ok {
941 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +0100942 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +0100943
944 // Extract information from the dependency. The exact information extracted
945 // is determined by the nature of the dependency which is determined by the tag.
946 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900947 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900948 })
949}
950
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900951func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000952 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
953 return nil
954 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900955 entriesList := module.Library.AndroidMkEntries()
956 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700957 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900958 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900959}
960
Jiyong Parkc678ad32018-04-10 13:07:10 +0900961// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900962func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900963 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900964}
965
Jiyong Parkc678ad32018-04-10 13:07:10 +0900966// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900967func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900968 return module.BaseModuleName() + sdkXmlFileSuffix
969}
970
Anton Hansson6bb88102020-03-27 19:43:19 +0000971// The dist path of the stub artifacts
972func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
973 if module.ModuleBase.Owner() != "" {
974 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
975 } else if Bool(module.sdkLibraryProperties.Core_lib) {
976 return path.Join("apistubs", "core", apiScope.name)
977 } else {
978 return path.Join("apistubs", "android", apiScope.name)
979 }
980}
981
Paul Duffin12ceb462019-12-24 20:31:31 +0000982// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +0100983func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +0100984 scopeProperties := module.scopeToProperties[apiScope]
985 if scopeProperties.Sdk_version != nil {
986 return proptools.String(scopeProperties.Sdk_version)
987 }
988
Paul Duffin12ceb462019-12-24 20:31:31 +0000989 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
990 if sdkDep.hasStandardLibs() {
991 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000992 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000993 } else {
994 // Otherwise, use no system module.
995 return "none"
996 }
997}
998
Paul Duffind1b3a922020-01-22 11:57:20 +0000999func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1000 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001001}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001002
Paul Duffind1b3a922020-01-22 11:57:20 +00001003func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1004 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001005}
1006
1007// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001008func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001009 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001010 Name *string
1011 Visibility []string
1012 Srcs []string
1013 Installable *bool
1014 Sdk_version *string
1015 System_modules *string
1016 Patch_module *string
1017 Libs []string
1018 Compile_dex *bool
1019 Java_version *string
1020 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001021 Pdk struct {
1022 Enabled *bool
1023 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001024 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001025 Openjdk9 struct {
1026 Srcs []string
1027 Javacflags []string
1028 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001029 Dist struct {
1030 Targets []string
1031 Dest *string
1032 Dir *string
1033 Tag *string
1034 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001035 }{}
1036
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001037 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +01001038
1039 // If stubs_library_visibility is not set then the created module will use the
1040 // visibility of this module.
1041 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1042 props.Visibility = visibility
1043
Jiyong Parkc678ad32018-04-10 13:07:10 +09001044 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001045 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001046 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001047 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001048 props.System_modules = module.deviceProperties.System_modules
1049 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001050 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001051 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssona9a31732020-05-21 10:38:30 +01001052 // The stub-annotations library contains special versions of the annotations
1053 // with CLASS retention policy, so that they're kept around for kotlin.
1054 props.Libs = append(props.Libs, "stub-annotations")
Jiyong Park82484c02018-04-23 21:41:26 +09001055 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001056 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1057 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
1058 props.Java_version = module.properties.Java_version
1059 if module.deviceProperties.Compile_dex != nil {
1060 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001061 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001062
Anton Hansson6bb88102020-03-27 19:43:19 +00001063 // Dist the class jar artifact for sdk builds.
1064 if !Bool(module.sdkLibraryProperties.No_dist) {
1065 props.Dist.Targets = []string{"sdk", "win_sdk"}
1066 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1067 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1068 props.Dist.Tag = proptools.StringPtr(".jar")
1069 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001070
Paul Duffin64e61992020-05-15 10:20:31 +01001071 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001072}
1073
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001074// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001075// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +01001076func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001077 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001078 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001079 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001080 Srcs []string
1081 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001082 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001083 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001084 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001085 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001086 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001087 Java_version *string
Anton Hanssona9a31732020-05-21 10:38:30 +01001088 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001089 Merge_annotations_dirs []string
1090 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001091 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001092 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001093 Current ApiToCheck
1094 Last_released ApiToCheck
1095 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001096
1097 Api_lint struct {
1098 Enabled *bool
1099 New_since *string
1100 Baseline_file *string
1101 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001102 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001103 Aidl struct {
1104 Include_dirs []string
1105 Local_include_dirs []string
1106 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001107 Dist struct {
1108 Targets []string
1109 Dest *string
1110 Dir *string
1111 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001112 }{}
1113
Paul Duffinda364252020-04-28 14:08:32 +01001114 // The stubs source processing uses the same compile time classpath when extracting the
1115 // API from the implementation library as it does when compiling it. i.e. the same
1116 // * sdk version
1117 // * system_modules
1118 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001119
Paul Duffina377e4c2020-04-29 13:30:54 +01001120 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001121
1122 // If stubs_source_visibility is not set then the created module will use the
1123 // visibility of this module.
1124 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1125 props.Visibility = visibility
1126
Paul Duffinc5d954a2020-05-16 18:54:24 +01001127 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1128 props.Sdk_version = module.deviceProperties.Sdk_version
1129 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001130 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001131 // A droiddoc module has only one Libs property and doesn't distinguish between
1132 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001133 props.Libs = module.properties.Libs
1134 props.Libs = append(props.Libs, module.properties.Static_libs...)
1135 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1136 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1137 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001138
Anton Hanssona9a31732020-05-21 10:38:30 +01001139 props.Annotations_enabled = proptools.BoolPtr(true)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001140 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1141 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1142
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001143 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001144 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001145 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001146 }
1147 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001148 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001149 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1150 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001151 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001152 disabledWarnings := []string{
1153 "MissingPermission",
1154 "BroadcastBehavior",
1155 "HiddenSuperclass",
1156 "DeprecationMismatch",
1157 "UnavailableSymbol",
1158 "SdkConstant",
1159 "HiddenTypeParameter",
1160 "Todo",
1161 "Typo",
1162 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001163 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001164
Paul Duffina377e4c2020-04-29 13:30:54 +01001165 if !createStubSources {
1166 // Stubs are not required.
1167 props.Generate_stubs = proptools.BoolPtr(false)
1168 }
1169
Paul Duffin3c7c3472020-04-07 18:50:10 +01001170 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001171 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001172 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001173 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001174
Paul Duffina377e4c2020-04-29 13:30:54 +01001175 if createApi {
1176 // List of APIs identified from the provided source files are created. They are later
1177 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1178 // last-released (a.k.a numbered) list of API.
1179 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1180 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1181 apiDir := module.getApiDir()
1182 currentApiFileName = path.Join(apiDir, currentApiFileName)
1183 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001184
Paul Duffina377e4c2020-04-29 13:30:54 +01001185 // check against the not-yet-release API
1186 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1187 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001188
Paul Duffina377e4c2020-04-29 13:30:54 +01001189 if !apiScope.unstable {
1190 // check against the latest released API
1191 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1192 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1193 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1194 module.latestRemovedApiFilegroupName(apiScope))
1195 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001196
Paul Duffina377e4c2020-04-29 13:30:54 +01001197 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1198 // Enable api lint.
1199 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1200 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001201
Paul Duffina377e4c2020-04-29 13:30:54 +01001202 // If it exists then pass a lint-baseline.txt through to droidstubs.
1203 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1204 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1205 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1206 if err != nil {
1207 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1208 }
1209 if len(paths) == 1 {
1210 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1211 } else if len(paths) != 0 {
1212 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1213 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001214 }
1215 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001216
Paul Duffina377e4c2020-04-29 13:30:54 +01001217 // Dist the api txt artifact for sdk builds.
1218 if !Bool(module.sdkLibraryProperties.No_dist) {
1219 props.Dist.Targets = []string{"sdk", "win_sdk"}
1220 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1221 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1222 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001223 }
1224
Colin Cross84dfc3d2019-09-25 11:33:01 -07001225 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001226}
1227
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001228func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1229 depTag := mctx.OtherModuleDependencyTag(dep)
1230 if depTag == xmlPermissionsFileTag {
1231 return true
1232 }
1233 return module.Library.DepIsInSameApex(mctx, dep)
1234}
1235
Jiyong Parkc678ad32018-04-10 13:07:10 +09001236// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001237func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001238 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001239 Name *string
1240 Lib_name *string
1241 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001242 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001243 Name: proptools.StringPtr(module.xmlFileName()),
1244 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1245 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001246 }
Jiyong Parke3833882020-02-17 17:28:10 +09001247
Jiyong Parke3833882020-02-17 17:28:10 +09001248 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001249}
1250
Paul Duffin50061512020-01-21 16:31:05 +00001251func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001252 var ver sdkVersion
1253 var kind sdkKind
1254 if s.usePrebuilt(ctx) {
1255 ver = s.version
1256 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001257 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001258 // We don't have prebuilt SDK for the specific sdkVersion.
1259 // Instead of breaking the build, fallback to use "system_current"
1260 ver = sdkVersionCurrent
1261 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001262 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001263
1264 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001265 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001266 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001267 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001268 if ctx.Config().AllowMissingDependencies() {
1269 return android.Paths{android.PathForSource(ctx, jar)}
1270 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001271 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001272 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001273 return nil
1274 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001275 return android.Paths{jarPath.Path()}
1276}
1277
Paul Duffin47624362020-05-20 12:19:10 +01001278func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001279
Paul Duffin47624362020-05-20 12:19:10 +01001280 // Check any special cases for java_sdk_library.
1281 if !sdkVersion.specified() {
Paul Duffind1b3a922020-01-22 11:57:20 +00001282 if headerJars {
Paul Duffin47624362020-05-20 12:19:10 +01001283 return module.HeaderJars()
Paul Duffind1b3a922020-01-22 11:57:20 +00001284 } else {
Paul Duffin47624362020-05-20 12:19:10 +01001285 return module.ImplementationJars()
Sundong Ahn054b19a2018-10-19 13:46:09 +09001286 }
Paul Duffin47624362020-05-20 12:19:10 +01001287 } else if sdkVersion.kind == sdkPrivate {
1288 return module.HeaderJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +09001289 }
Paul Duffin47624362020-05-20 12:19:10 +01001290
Paul Duffina3fb67d2020-05-20 14:20:02 +01001291 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001292}
1293
Sundong Ahn241cd372018-07-13 16:16:44 +09001294// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001295func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1296 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1297}
1298
1299// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001300func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001301 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001302}
1303
Sundong Ahn80a87b32019-05-13 15:02:50 +09001304func (module *SdkLibrary) SetNoDist() {
1305 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1306}
1307
Colin Cross571cccf2019-02-04 11:22:08 -08001308var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1309
Jiyong Park82484c02018-04-23 21:41:26 +09001310func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001311 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001312 return &[]string{}
1313 }).(*[]string)
1314}
1315
Paul Duffin749f98f2019-12-30 17:23:46 +00001316func (module *SdkLibrary) getApiDir() string {
1317 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1318}
1319
Jiyong Parkc678ad32018-04-10 13:07:10 +09001320// For a java_sdk_library module, create internal modules for stubs, docs,
1321// runtime libs and xml file. If requested, the stubs and docs are created twice
1322// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001323func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1324 // If the module has been disabled then don't create any child modules.
1325 if !module.Enabled() {
1326 return
1327 }
1328
Paul Duffinc5d954a2020-05-16 18:54:24 +01001329 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001330 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001331 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001332 }
1333
Paul Duffin37e0b772019-12-30 17:20:10 +00001334 // If this builds against standard libraries (i.e. is not part of the core libraries)
1335 // then assume it provides both system and test apis. Otherwise, assume it does not and
1336 // also assume it does not contribute to the dist build.
1337 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1338 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001339 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001340 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1341
Inseob Kim8098faa2019-03-18 10:19:51 +09001342 missing_current_api := false
1343
Paul Duffin3a254982020-04-28 10:44:03 +01001344 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001345
Paul Duffin749f98f2019-12-30 17:23:46 +00001346 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001347 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001348 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001349 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001350 p := android.ExistentPathForSource(mctx, path)
1351 if !p.Valid() {
1352 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1353 missing_current_api = true
1354 }
1355 }
1356 }
1357
1358 if missing_current_api {
1359 script := "build/soong/scripts/gen-java-current-api-files.sh"
1360 p := android.ExistentPathForSource(mctx, script)
1361
1362 if !p.Valid() {
1363 panic(fmt.Sprintf("script file %s doesn't exist", script))
1364 }
1365
1366 mctx.ModuleErrorf("One or more current api files are missing. "+
1367 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001368 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001369 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001370 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001371 return
1372 }
1373
Paul Duffin3a254982020-04-28 10:44:03 +01001374 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001375 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001376 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001377
1378 // If the args needed to generate the stubs and API are the same then they
1379 // can be generated in a single invocation of metalava, otherwise they will
1380 // need separate invocations.
1381 if scope.createStubsSourceAndApiTogether {
1382 // Use the stubs source name for legacy reasons.
1383 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1384 } else {
1385 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1386
1387 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001388 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001389 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1390 }
1391
Paul Duffind1b3a922020-01-22 11:57:20 +00001392 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001393 }
1394
Paul Duffin43db9be2019-12-30 17:35:49 +00001395 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
1396 // for runtime
1397 module.createXmlFile(mctx)
1398
1399 // record java_sdk_library modules so that they are exported to make
1400 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1401 javaSdkLibrariesLock.Lock()
1402 defer javaSdkLibrariesLock.Unlock()
1403 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1404 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001405}
1406
1407func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001408 module.AddProperties(
1409 &module.sdkLibraryProperties,
Paul Duffinc5d954a2020-05-16 18:54:24 +01001410 &module.properties,
1411 &module.dexpreoptProperties,
1412 &module.deviceProperties,
1413 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001414 )
1415
Paul Duffin64e61992020-05-15 10:20:31 +01001416 module.initSdkLibraryComponent(&module.ModuleBase)
1417
Paul Duffinc5d954a2020-05-16 18:54:24 +01001418 module.properties.Installable = proptools.BoolPtr(true)
1419 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001420}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001421
Paul Duffin1a724e62020-05-08 13:44:43 +01001422// Defines how to name the individual component modules the sdk library creates.
1423type sdkLibraryComponentNamingScheme interface {
1424 stubsLibraryModuleName(scope *apiScope, baseName string) string
1425
1426 stubsSourceModuleName(scope *apiScope, baseName string) string
1427
1428 apiModuleName(scope *apiScope, baseName string) string
1429}
1430
1431type defaultNamingScheme struct {
1432}
1433
1434func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1435 return scope.stubsLibraryModuleName(baseName)
1436}
1437
1438func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1439 return scope.stubsSourceModuleName(baseName)
1440}
1441
1442func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1443 return scope.apiModuleName(baseName)
1444}
1445
1446var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1447
Paul Duffindef8a892020-05-08 15:36:30 +01001448type frameworkModulesNamingScheme struct {
1449}
1450
1451func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1452 suffix := scope.name
1453 if scope == apiScopeModuleLib {
1454 suffix = "module_libs_"
1455 }
1456 return suffix
1457}
1458
1459func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1460 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1461}
1462
1463func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1464 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1465}
1466
1467func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1468 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1469}
1470
1471var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1472
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001473// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1474// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1475// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1476// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1477// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001478func SdkLibraryFactory() android.Module {
1479 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001480
1481 // Initialize information common between source and prebuilt.
1482 module.initCommon(&module.ModuleBase)
1483
Inseob Kimc0907f12019-02-08 21:00:45 +09001484 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001485 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001486 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001487
1488 // Initialize the map from scope to scope specific properties.
1489 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1490 for _, scope := range allApiScopes {
1491 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1492 }
1493 module.scopeToProperties = scopeToProperties
1494
Paul Duffin344c4ee2020-04-29 23:35:13 +01001495 // Add the properties containing visibility rules so that they are checked.
1496 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1497 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1498
Paul Duffin1a724e62020-05-08 13:44:43 +01001499 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
1500 if module.initCommonAfterDefaultsApplied(ctx) {
1501 module.CreateInternalModules(ctx)
1502 }
1503 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001504 return module
1505}
Colin Cross79c7c262019-04-17 11:11:46 -07001506
1507//
1508// SDK library prebuilts
1509//
1510
Paul Duffin56d44902020-01-31 13:36:25 +00001511// Properties associated with each api scope.
1512type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001513 Jars []string `android:"path"`
1514
1515 Sdk_version *string
1516
Colin Cross79c7c262019-04-17 11:11:46 -07001517 // List of shared java libs that this module has dependencies to
1518 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001519
Paul Duffin5fb82132020-04-29 20:45:27 +01001520 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001521 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001522
1523 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001524 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001525
1526 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001527 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001528}
1529
Paul Duffin56d44902020-01-31 13:36:25 +00001530type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001531 // List of shared java libs, common to all scopes, that this module has
1532 // dependencies to
1533 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001534}
1535
Colin Cross79c7c262019-04-17 11:11:46 -07001536type sdkLibraryImport struct {
1537 android.ModuleBase
1538 android.DefaultableModuleBase
1539 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001540 android.ApexModuleBase
1541 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001542
1543 properties sdkLibraryImportProperties
1544
Paul Duffin6a2bd112020-04-07 19:27:04 +01001545 // Map from api scope to the scope specific property structure.
1546 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1547
Paul Duffin56d44902020-01-31 13:36:25 +00001548 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001549}
1550
1551var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1552
Paul Duffin6a2bd112020-04-07 19:27:04 +01001553// The type of a structure that contains a field of type sdkLibraryScopeProperties
1554// for each apiscope in allApiScopes, e.g. something like:
1555// struct {
1556// Public sdkLibraryScopeProperties
1557// System sdkLibraryScopeProperties
1558// ...
1559// }
1560var allScopeStructType = createAllScopePropertiesStructType()
1561
1562// Dynamically create a structure type for each apiscope in allApiScopes.
1563func createAllScopePropertiesStructType() reflect.Type {
1564 var fields []reflect.StructField
1565 for _, apiScope := range allApiScopes {
1566 field := reflect.StructField{
1567 Name: apiScope.fieldName,
1568 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1569 }
1570 fields = append(fields, field)
1571 }
1572
1573 return reflect.StructOf(fields)
1574}
1575
1576// Create an instance of the scope specific structure type and return a map
1577// from apiscope to a pointer to each scope specific field.
1578func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1579 allScopePropertiesPtr := reflect.New(allScopeStructType)
1580 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1581 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1582
1583 for _, apiScope := range allApiScopes {
1584 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1585 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1586 }
1587
1588 return allScopePropertiesPtr.Interface(), scopeProperties
1589}
1590
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001591// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001592func sdkLibraryImportFactory() android.Module {
1593 module := &sdkLibraryImport{}
1594
Paul Duffin6a2bd112020-04-07 19:27:04 +01001595 allScopeProperties, scopeToProperties := createPropertiesInstance()
1596 module.scopeProperties = scopeToProperties
1597 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001598
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001599 // Initialize information common between source and prebuilt.
1600 module.initCommon(&module.ModuleBase)
1601
Paul Duffin0bdcb272020-02-06 15:24:57 +00001602 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001603 android.InitApexModule(module)
1604 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001605 InitJavaModule(module, android.HostAndDeviceSupported)
1606
Paul Duffin1a724e62020-05-08 13:44:43 +01001607 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1608 if module.initCommonAfterDefaultsApplied(mctx) {
1609 module.createInternalModules(mctx)
1610 }
1611 })
Colin Cross79c7c262019-04-17 11:11:46 -07001612 return module
1613}
1614
1615func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1616 return &module.prebuilt
1617}
1618
1619func (module *sdkLibraryImport) Name() string {
1620 return module.prebuilt.Name(module.ModuleBase.Name())
1621}
1622
Paul Duffinbf735aa2020-05-08 15:01:19 +01001623func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001624
Paul Duffin50061512020-01-21 16:31:05 +00001625 // If the build is configured to use prebuilts then force this to be preferred.
1626 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1627 module.prebuilt.ForcePrefer()
1628 }
1629
Paul Duffin6a2bd112020-04-07 19:27:04 +01001630 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001631 if len(scopeProperties.Jars) == 0 {
1632 continue
1633 }
1634
Paul Duffinf6155722020-04-09 00:07:11 +01001635 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001636
Paul Duffin533f9c72020-05-20 16:18:00 +01001637 if len(scopeProperties.Stub_srcs) > 0 {
1638 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1639 }
Paul Duffin56d44902020-01-31 13:36:25 +00001640 }
Colin Cross79c7c262019-04-17 11:11:46 -07001641
1642 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1643 javaSdkLibrariesLock.Lock()
1644 defer javaSdkLibrariesLock.Unlock()
1645 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1646}
1647
Paul Duffinbf735aa2020-05-08 15:01:19 +01001648func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001649 // Creates a java import for the jar with ".stubs" suffix
1650 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001651 Name *string
1652 Sdk_version *string
1653 Libs []string
1654 Jars []string
1655 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001656 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001657 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001658 props.Sdk_version = scopeProperties.Sdk_version
1659 // Prepend any of the libs from the legacy public properties to the libs for each of the
1660 // scopes to avoid having to duplicate them in each scope.
1661 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1662 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001663
Paul Duffindd89a282020-05-13 16:08:09 +01001664 // The imports are preferred if the java_sdk_library_import is preferred.
1665 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001666
1667 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001668}
1669
Paul Duffinbf735aa2020-05-08 15:01:19 +01001670func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001671 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001672 Name *string
1673 Srcs []string
1674 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001675 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001676 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001677 props.Srcs = scopeProperties.Stub_srcs
1678 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001679
1680 // The stubs source is preferred if the java_sdk_library_import is preferred.
1681 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001682}
1683
Colin Cross79c7c262019-04-17 11:11:46 -07001684func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001685 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001686 if len(scopeProperties.Jars) == 0 {
1687 continue
1688 }
1689
1690 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001691 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001692
1693 if len(scopeProperties.Stub_srcs) > 0 {
1694 // Add dependencies to the prebuilt stubs source library
1695 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1696 }
Paul Duffin56d44902020-01-31 13:36:25 +00001697 }
Colin Cross79c7c262019-04-17 11:11:46 -07001698}
1699
Paul Duffin46fdda82020-05-14 15:39:10 +01001700func (module *sdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
1701 return module.commonOutputFiles(tag)
1702}
1703
Colin Cross79c7c262019-04-17 11:11:46 -07001704func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001705 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001706 ctx.VisitDirectDeps(func(to android.Module) {
1707 tag := ctx.OtherModuleDependencyTag(to)
1708
Paul Duffin533f9c72020-05-20 16:18:00 +01001709 // Extract information from any of the scope specific dependencies.
1710 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1711 apiScope := scopeTag.apiScope
1712 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1713
1714 // Extract information from the dependency. The exact information extracted
1715 // is determined by the nature of the dependency which is determined by the tag.
1716 scopeTag.extractDepInfo(ctx, to, scopePaths)
Colin Cross79c7c262019-04-17 11:11:46 -07001717 }
1718 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001719
1720 // Populate the scope paths with information from the properties.
1721 for apiScope, scopeProperties := range module.scopeProperties {
1722 if len(scopeProperties.Jars) == 0 {
1723 continue
1724 }
1725
1726 paths := module.getScopePathsCreateIfNeeded(apiScope)
1727 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1728 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1729 }
Colin Cross79c7c262019-04-17 11:11:46 -07001730}
1731
Paul Duffin47624362020-05-20 12:19:10 +01001732func (module *sdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffina3fb67d2020-05-20 14:20:02 +01001733 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001734}
1735
Colin Cross79c7c262019-04-17 11:11:46 -07001736// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001737func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001738 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001739 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001740}
1741
1742// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001743func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001744 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001745 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001746}
Jiyong Parke3833882020-02-17 17:28:10 +09001747
1748//
1749// java_sdk_library_xml
1750//
1751type sdkLibraryXml struct {
1752 android.ModuleBase
1753 android.DefaultableModuleBase
1754 android.ApexModuleBase
1755
1756 properties sdkLibraryXmlProperties
1757
1758 outputFilePath android.OutputPath
1759 installDirPath android.InstallPath
1760}
1761
1762type sdkLibraryXmlProperties struct {
1763 // canonical name of the lib
1764 Lib_name *string
1765}
1766
1767// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1768// Not to be used directly by users. java_sdk_library internally uses this.
1769func sdkLibraryXmlFactory() android.Module {
1770 module := &sdkLibraryXml{}
1771
1772 module.AddProperties(&module.properties)
1773
1774 android.InitApexModule(module)
1775 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1776
1777 return module
1778}
1779
1780// from android.PrebuiltEtcModule
1781func (module *sdkLibraryXml) SubDir() string {
1782 return "permissions"
1783}
1784
1785// from android.PrebuiltEtcModule
1786func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1787 return module.outputFilePath
1788}
1789
1790// from android.ApexModule
1791func (module *sdkLibraryXml) AvailableFor(what string) bool {
1792 return true
1793}
1794
1795func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1796 // do nothing
1797}
1798
1799// File path to the runtime implementation library
1800func (module *sdkLibraryXml) implPath() string {
1801 implName := proptools.String(module.properties.Lib_name)
1802 if apexName := module.ApexName(); apexName != "" {
1803 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1804 // In most cases, this works fine. But when apex_name is set or override_apex is used
1805 // this can be wrong.
1806 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1807 }
1808 partition := "system"
1809 if module.SocSpecific() {
1810 partition = "vendor"
1811 } else if module.DeviceSpecific() {
1812 partition = "odm"
1813 } else if module.ProductSpecific() {
1814 partition = "product"
1815 } else if module.SystemExtSpecific() {
1816 partition = "system_ext"
1817 }
1818 return "/" + partition + "/framework/" + implName + ".jar"
1819}
1820
1821func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1822 libName := proptools.String(module.properties.Lib_name)
1823 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1824
1825 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1826 rule := android.NewRuleBuilder()
1827 rule.Command().
1828 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1829 Output(module.outputFilePath)
1830
1831 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1832
1833 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1834}
1835
1836func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1837 if !module.IsForPlatform() {
1838 return []android.AndroidMkEntries{android.AndroidMkEntries{
1839 Disabled: true,
1840 }}
1841 }
1842
1843 return []android.AndroidMkEntries{android.AndroidMkEntries{
1844 Class: "ETC",
1845 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1846 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1847 func(entries *android.AndroidMkEntries) {
1848 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1849 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1850 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1851 },
1852 },
1853 }}
1854}
Paul Duffin61871622020-02-10 13:37:10 +00001855
1856type sdkLibrarySdkMemberType struct {
1857 android.SdkMemberTypeBase
1858}
1859
1860func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1861 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1862}
1863
1864func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1865 _, ok := module.(*SdkLibrary)
1866 return ok
1867}
1868
1869func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1870 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1871}
1872
1873func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1874 return &sdkLibrarySdkMemberProperties{}
1875}
1876
1877type sdkLibrarySdkMemberProperties struct {
1878 android.SdkMemberPropertiesBase
1879
1880 // Scope to per scope properties.
1881 Scopes map[*apiScope]scopeProperties
1882
1883 // Additional libraries that the exported stubs libraries depend upon.
1884 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001885
1886 // The Java stubs source files.
1887 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01001888
1889 // The naming scheme.
1890 Naming_scheme *string
Paul Duffin61871622020-02-10 13:37:10 +00001891}
1892
1893type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01001894 Jars android.Paths
1895 StubsSrcJar android.Path
1896 CurrentApiFile android.Path
1897 RemovedApiFile android.Path
1898 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00001899}
1900
1901func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1902 sdk := variant.(*SdkLibrary)
1903
1904 s.Scopes = make(map[*apiScope]scopeProperties)
1905 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01001906 paths := sdk.findScopePaths(apiScope)
1907 if paths == nil {
1908 continue
1909 }
1910
Paul Duffin61871622020-02-10 13:37:10 +00001911 jars := paths.stubsImplPath
1912 if len(jars) > 0 {
1913 properties := scopeProperties{}
1914 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01001915 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01001916 properties.StubsSrcJar = paths.stubsSrcJar.Path()
1917 properties.CurrentApiFile = paths.currentApiFilePath.Path()
1918 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffin61871622020-02-10 13:37:10 +00001919 s.Scopes[apiScope] = properties
1920 }
1921 }
1922
1923 s.Libs = sdk.properties.Libs
Paul Duffinf8e08b22020-05-13 16:54:55 +01001924 s.Naming_scheme = sdk.commonProperties.Naming_scheme
Paul Duffin61871622020-02-10 13:37:10 +00001925}
1926
1927func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01001928 if s.Naming_scheme != nil {
1929 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
1930 }
1931
Paul Duffin61871622020-02-10 13:37:10 +00001932 for _, apiScope := range allApiScopes {
1933 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01001934 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00001935
Paul Duffinf488ef22020-04-09 00:10:17 +01001936 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1937
Paul Duffin61871622020-02-10 13:37:10 +00001938 var jars []string
1939 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01001940 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00001941 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1942 jars = append(jars, dest)
1943 }
1944 scopeSet.AddProperty("jars", jars)
1945
Paul Duffinf488ef22020-04-09 00:10:17 +01001946 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1947 // the source files are also unpacked.
1948 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1949 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1950 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1951
Paul Duffin75dcc802020-04-09 01:08:11 +01001952 if properties.CurrentApiFile != nil {
1953 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1954 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1955 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1956 }
1957
1958 if properties.RemovedApiFile != nil {
1959 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1960 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1961 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1962 }
1963
Paul Duffin61871622020-02-10 13:37:10 +00001964 if properties.SdkVersion != "" {
1965 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1966 }
1967 }
1968 }
1969
1970 if len(s.Libs) > 0 {
1971 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1972 }
1973}