blob: b215a76f770d91a4b02b06ed85e150827cb31a63 [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 Duffin56d44902020-01-31 13:36:25 +0000546}
547
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100548func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
549 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100550
551 moduleBase.AddProperties(&c.commonProperties)
552}
553
554func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
555 schemeProperty := proptools.StringDefault(c.commonProperties.Naming_scheme, "default")
556 switch schemeProperty {
557 case "default":
558 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100559 case "framework-modules":
560 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100561 default:
562 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
563 return false
564 }
565
566 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100567}
568
569// Name of the java_library module that compiles the stubs source.
570func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100571 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100572}
573
574// Name of the droidstubs module that generates the stubs source and may also
575// generate/check the API.
576func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100577 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100578}
579
580// Name of the droidstubs module that generates/checks the API. Only used if it
581// requires different arts to the stubs source generating module.
582func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100583 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100584}
585
Paul Duffin46fdda82020-05-14 15:39:10 +0100586// The component names for different outputs of the java_sdk_library.
587//
588// They are similar to the names used for the child modules it creates
589const (
590 stubsSourceComponentName = "stubs.source"
591
592 apiTxtComponentName = "api.txt"
593
594 removedApiTxtComponentName = "removed-api.txt"
595)
596
597// A regular expression to match tags that reference a specific stubs component.
598//
599// It will only match if given a valid scope and a valid component. It is verfy strict
600// to ensure it does not accidentally match a similar looking tag that should be processed
601// by the embedded Library.
602var tagSplitter = func() *regexp.Regexp {
603 // Given a list of literal string items returns a regular expression that will
604 // match any one of the items.
605 choice := func(items ...string) string {
606 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
607 }
608
609 // Regular expression to match one of the scopes.
610 scopesRegexp := choice(allScopeNames...)
611
612 // Regular expression to match one of the components.
613 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
614
615 // Regular expression to match any combination of one scope and one component.
616 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
617}()
618
619// For OutputFileProducer interface
620//
621// .<scope>.stubs.source
622// .<scope>.api.txt
623// .<scope>.removed-api.txt
624func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
625 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
626 scopeName := groups[1]
627 component := groups[2]
628
629 if scope, ok := scopeByName[scopeName]; ok {
630 paths := c.findScopePaths(scope)
631 if paths == nil {
632 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
633 }
634
635 switch component {
636 case stubsSourceComponentName:
637 if paths.stubsSrcJar.Valid() {
638 return android.Paths{paths.stubsSrcJar.Path()}, nil
639 }
640
641 case apiTxtComponentName:
642 if paths.currentApiFilePath.Valid() {
643 return android.Paths{paths.currentApiFilePath.Path()}, nil
644 }
645
646 case removedApiTxtComponentName:
647 if paths.removedApiFilePath.Valid() {
648 return android.Paths{paths.removedApiFilePath.Path()}, nil
649 }
650 }
651
652 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
653 } else {
654 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
655 }
656
657 } else {
658 return nil, nil
659 }
660}
661
Paul Duffin5ae30792020-05-20 11:52:25 +0100662func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000663 if c.scopePaths == nil {
664 c.scopePaths = make(map[*apiScope]*scopePaths)
665 }
666 paths := c.scopePaths[scope]
667 if paths == nil {
668 paths = &scopePaths{}
669 c.scopePaths[scope] = paths
670 }
671
672 return paths
673}
674
Paul Duffin5ae30792020-05-20 11:52:25 +0100675func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
676 if c.scopePaths == nil {
677 return nil
678 }
679
680 return c.scopePaths[scope]
681}
682
683// If this does not support the requested api scope then find the closest available
684// scope it does support. Returns nil if no such scope is available.
685func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
686 for s := scope; s != nil; s = s.extends {
687 if paths := c.findScopePaths(s); paths != nil {
688 return paths
689 }
690 }
691
692 // This should never happen outside tests as public should be the base scope for every
693 // scope and is enabled by default.
694 return nil
695}
696
Paul Duffina3fb67d2020-05-20 14:20:02 +0100697func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-05-20 12:19:10 +0100698
699 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
700 if sdkVersion.version.isNumbered() {
701 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
702 }
703
704 var apiScope *apiScope
705 switch sdkVersion.kind {
706 case sdkSystem:
707 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100708 case sdkModule:
709 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100710 case sdkTest:
711 apiScope = apiScopeTest
712 default:
713 apiScope = apiScopePublic
714 }
715
Paul Duffin5ae30792020-05-20 11:52:25 +0100716 paths := c.findClosestScopePath(apiScope)
717 if paths == nil {
718 var scopes []string
719 for _, s := range allApiScopes {
720 if c.findScopePaths(s) != nil {
721 scopes = append(scopes, s.name)
722 }
723 }
724 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
725 return nil
726 }
727
Paul Duffina3fb67d2020-05-20 14:20:02 +0100728 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100729}
730
Inseob Kimc0907f12019-02-08 21:00:45 +0900731type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900732 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900733
Sundong Ahn054b19a2018-10-19 13:46:09 +0900734 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900735
Paul Duffin3a254982020-04-28 10:44:03 +0100736 // Map from api scope to the scope specific property structure.
737 scopeToProperties map[*apiScope]*ApiScopeProperties
738
Paul Duffin56d44902020-01-31 13:36:25 +0000739 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900740}
741
Inseob Kimc0907f12019-02-08 21:00:45 +0900742var _ Dependency = (*SdkLibrary)(nil)
743var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800744
Paul Duffin3a254982020-04-28 10:44:03 +0100745func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
746 return module.sdkLibraryProperties.Generate_system_and_test_apis
747}
748
749func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
750 // Check to see if any scopes have been explicitly enabled. If any have then all
751 // must be.
752 anyScopesExplicitlyEnabled := false
753 for _, scope := range allApiScopes {
754 scopeProperties := module.scopeToProperties[scope]
755 if scopeProperties.Enabled != nil {
756 anyScopesExplicitlyEnabled = true
757 break
758 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000759 }
Paul Duffin3a254982020-04-28 10:44:03 +0100760
761 var generatedScopes apiScopes
762 enabledScopes := make(map[*apiScope]struct{})
763 for _, scope := range allApiScopes {
764 scopeProperties := module.scopeToProperties[scope]
765 // If any scopes are explicitly enabled then ignore the legacy enabled status.
766 // This is to ensure that any new usages of this module type do not rely on legacy
767 // behaviour.
768 defaultEnabledStatus := false
769 if anyScopesExplicitlyEnabled {
770 defaultEnabledStatus = scope.defaultEnabledStatus
771 } else {
772 defaultEnabledStatus = scope.legacyEnabledStatus(module)
773 }
774 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
775 if enabled {
776 enabledScopes[scope] = struct{}{}
777 generatedScopes = append(generatedScopes, scope)
778 }
779 }
780
781 // Now check to make sure that any scope that is extended by an enabled scope is also
782 // enabled.
783 for _, scope := range allApiScopes {
784 if _, ok := enabledScopes[scope]; ok {
785 extends := scope.extends
786 if extends != nil {
787 if _, ok := enabledScopes[extends]; !ok {
788 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
789 }
790 }
791 }
792 }
793
794 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000795}
796
Paul Duffine74ac732020-02-06 13:51:46 +0000797var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
798
Jiyong Parke3833882020-02-17 17:28:10 +0900799func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
800 if dt, ok := depTag.(dependencyTag); ok {
801 return dt == xmlPermissionsFileTag
802 }
803 return false
804}
805
Inseob Kimc0907f12019-02-08 21:00:45 +0900806func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100807 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000808 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100809 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000810
Paul Duffina377e4c2020-04-29 13:30:54 +0100811 // If the stubs source and API cannot be generated together then add an additional dependency on
812 // the API module.
813 if apiScope.createStubsSourceAndApiTogether {
814 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100815 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100816 } else {
817 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100818 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
819 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100820 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900821 }
822
Paul Duffine74ac732020-02-06 13:51:46 +0000823 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
824 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900825 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000826 }
827
Sundong Ahn054b19a2018-10-19 13:46:09 +0900828 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900829}
830
Paul Duffin46fdda82020-05-14 15:39:10 +0100831func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
832 paths, err := module.commonOutputFiles(tag)
833 if paths == nil && err == nil {
834 return module.Library.OutputFiles(tag)
835 } else {
836 return paths, err
837 }
838}
839
Inseob Kimc0907f12019-02-08 21:00:45 +0900840func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000841 // Don't build an implementation library if this is api only.
842 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
843 module.Library.GenerateAndroidBuildActions(ctx)
844 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900845
Sundong Ahn57368eb2018-07-06 11:20:23 +0900846 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000847 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900848 // the recorded paths will be returned depending on the link type of the caller.
849 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900850 tag := ctx.OtherModuleDependencyTag(to)
851
Paul Duffin5fb82132020-04-29 20:45:27 +0100852 // Extract information from any of the scope specific dependencies.
853 if scopeTag, ok := tag.(scopeDependencyTag); ok {
854 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +0100855 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +0100856
857 // Extract information from the dependency. The exact information extracted
858 // is determined by the nature of the dependency which is determined by the tag.
859 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900860 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900861 })
862}
863
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900864func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000865 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
866 return nil
867 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900868 entriesList := module.Library.AndroidMkEntries()
869 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700870 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900871 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900872}
873
Jiyong Parkc678ad32018-04-10 13:07:10 +0900874// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900875func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900876 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900877}
878
Jiyong Parkc678ad32018-04-10 13:07:10 +0900879// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900880func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900881 return module.BaseModuleName() + sdkXmlFileSuffix
882}
883
Anton Hansson6bb88102020-03-27 19:43:19 +0000884// The dist path of the stub artifacts
885func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
886 if module.ModuleBase.Owner() != "" {
887 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
888 } else if Bool(module.sdkLibraryProperties.Core_lib) {
889 return path.Join("apistubs", "core", apiScope.name)
890 } else {
891 return path.Join("apistubs", "android", apiScope.name)
892 }
893}
894
Paul Duffin12ceb462019-12-24 20:31:31 +0000895// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +0100896func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +0100897 scopeProperties := module.scopeToProperties[apiScope]
898 if scopeProperties.Sdk_version != nil {
899 return proptools.String(scopeProperties.Sdk_version)
900 }
901
Paul Duffin12ceb462019-12-24 20:31:31 +0000902 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
903 if sdkDep.hasStandardLibs() {
904 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000905 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000906 } else {
907 // Otherwise, use no system module.
908 return "none"
909 }
910}
911
Paul Duffind1b3a922020-01-22 11:57:20 +0000912func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
913 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900914}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900915
Paul Duffind1b3a922020-01-22 11:57:20 +0000916func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
917 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900918}
919
920// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +0100921func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900922 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +0100923 Name *string
924 Visibility []string
925 Srcs []string
926 Installable *bool
927 Sdk_version *string
928 System_modules *string
929 Patch_module *string
930 Libs []string
931 Compile_dex *bool
932 Java_version *string
933 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900934 Pdk struct {
935 Enabled *bool
936 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900937 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900938 Openjdk9 struct {
939 Srcs []string
940 Javacflags []string
941 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000942 Dist struct {
943 Targets []string
944 Dest *string
945 Dir *string
946 Tag *string
947 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900948 }{}
949
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100950 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +0100951
952 // If stubs_library_visibility is not set then the created module will use the
953 // visibility of this module.
954 visibility := module.sdkLibraryProperties.Stubs_library_visibility
955 props.Visibility = visibility
956
Jiyong Parkc678ad32018-04-10 13:07:10 +0900957 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100958 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000959 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100960 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +0100961 props.System_modules = module.deviceProperties.System_modules
962 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000963 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900964 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssona9a31732020-05-21 10:38:30 +0100965 // The stub-annotations library contains special versions of the annotations
966 // with CLASS retention policy, so that they're kept around for kotlin.
967 props.Libs = append(props.Libs, "stub-annotations")
Jiyong Park82484c02018-04-23 21:41:26 +0900968 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +0100969 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
970 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
971 props.Java_version = module.properties.Java_version
972 if module.deviceProperties.Compile_dex != nil {
973 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900974 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900975
Anton Hansson6bb88102020-03-27 19:43:19 +0000976 // Dist the class jar artifact for sdk builds.
977 if !Bool(module.sdkLibraryProperties.No_dist) {
978 props.Dist.Targets = []string{"sdk", "win_sdk"}
979 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
980 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
981 props.Dist.Tag = proptools.StringPtr(".jar")
982 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900983
Colin Cross84dfc3d2019-09-25 11:33:01 -0700984 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900985}
986
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100987// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +0100988// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +0100989func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900990 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900991 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +0100992 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900993 Srcs []string
994 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100995 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000996 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900997 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000998 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900999 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001000 Java_version *string
Anton Hanssona9a31732020-05-21 10:38:30 +01001001 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001002 Merge_annotations_dirs []string
1003 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001004 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001005 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001006 Current ApiToCheck
1007 Last_released ApiToCheck
1008 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001009
1010 Api_lint struct {
1011 Enabled *bool
1012 New_since *string
1013 Baseline_file *string
1014 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001015 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001016 Aidl struct {
1017 Include_dirs []string
1018 Local_include_dirs []string
1019 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001020 Dist struct {
1021 Targets []string
1022 Dest *string
1023 Dir *string
1024 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001025 }{}
1026
Paul Duffinda364252020-04-28 14:08:32 +01001027 // The stubs source processing uses the same compile time classpath when extracting the
1028 // API from the implementation library as it does when compiling it. i.e. the same
1029 // * sdk version
1030 // * system_modules
1031 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001032
Paul Duffina377e4c2020-04-29 13:30:54 +01001033 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001034
1035 // If stubs_source_visibility is not set then the created module will use the
1036 // visibility of this module.
1037 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1038 props.Visibility = visibility
1039
Paul Duffinc5d954a2020-05-16 18:54:24 +01001040 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1041 props.Sdk_version = module.deviceProperties.Sdk_version
1042 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001043 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001044 // A droiddoc module has only one Libs property and doesn't distinguish between
1045 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001046 props.Libs = module.properties.Libs
1047 props.Libs = append(props.Libs, module.properties.Static_libs...)
1048 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1049 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1050 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001051
Anton Hanssona9a31732020-05-21 10:38:30 +01001052 props.Annotations_enabled = proptools.BoolPtr(true)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001053 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1054 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1055
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001056 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001057 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001058 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001059 }
1060 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001061 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001062 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1063 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001064 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001065 disabledWarnings := []string{
1066 "MissingPermission",
1067 "BroadcastBehavior",
1068 "HiddenSuperclass",
1069 "DeprecationMismatch",
1070 "UnavailableSymbol",
1071 "SdkConstant",
1072 "HiddenTypeParameter",
1073 "Todo",
1074 "Typo",
1075 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001076 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001077
Paul Duffina377e4c2020-04-29 13:30:54 +01001078 if !createStubSources {
1079 // Stubs are not required.
1080 props.Generate_stubs = proptools.BoolPtr(false)
1081 }
1082
Paul Duffin3c7c3472020-04-07 18:50:10 +01001083 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001084 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001085 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001086 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001087
Paul Duffina377e4c2020-04-29 13:30:54 +01001088 if createApi {
1089 // List of APIs identified from the provided source files are created. They are later
1090 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1091 // last-released (a.k.a numbered) list of API.
1092 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1093 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1094 apiDir := module.getApiDir()
1095 currentApiFileName = path.Join(apiDir, currentApiFileName)
1096 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001097
Paul Duffina377e4c2020-04-29 13:30:54 +01001098 // check against the not-yet-release API
1099 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1100 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001101
Paul Duffina377e4c2020-04-29 13:30:54 +01001102 if !apiScope.unstable {
1103 // check against the latest released API
1104 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1105 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1106 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1107 module.latestRemovedApiFilegroupName(apiScope))
1108 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001109
Paul Duffina377e4c2020-04-29 13:30:54 +01001110 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1111 // Enable api lint.
1112 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1113 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001114
Paul Duffina377e4c2020-04-29 13:30:54 +01001115 // If it exists then pass a lint-baseline.txt through to droidstubs.
1116 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1117 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1118 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1119 if err != nil {
1120 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1121 }
1122 if len(paths) == 1 {
1123 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1124 } else if len(paths) != 0 {
1125 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1126 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001127 }
1128 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001129
Paul Duffina377e4c2020-04-29 13:30:54 +01001130 // Dist the api txt artifact for sdk builds.
1131 if !Bool(module.sdkLibraryProperties.No_dist) {
1132 props.Dist.Targets = []string{"sdk", "win_sdk"}
1133 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1134 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1135 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001136 }
1137
Colin Cross84dfc3d2019-09-25 11:33:01 -07001138 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001139}
1140
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001141func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1142 depTag := mctx.OtherModuleDependencyTag(dep)
1143 if depTag == xmlPermissionsFileTag {
1144 return true
1145 }
1146 return module.Library.DepIsInSameApex(mctx, dep)
1147}
1148
Jiyong Parkc678ad32018-04-10 13:07:10 +09001149// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001150func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001151 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001152 Name *string
1153 Lib_name *string
1154 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001155 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001156 Name: proptools.StringPtr(module.xmlFileName()),
1157 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1158 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001159 }
Jiyong Parke3833882020-02-17 17:28:10 +09001160
Jiyong Parke3833882020-02-17 17:28:10 +09001161 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001162}
1163
Paul Duffin50061512020-01-21 16:31:05 +00001164func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001165 var ver sdkVersion
1166 var kind sdkKind
1167 if s.usePrebuilt(ctx) {
1168 ver = s.version
1169 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001170 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001171 // We don't have prebuilt SDK for the specific sdkVersion.
1172 // Instead of breaking the build, fallback to use "system_current"
1173 ver = sdkVersionCurrent
1174 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001175 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001176
1177 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001178 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001179 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001180 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001181 if ctx.Config().AllowMissingDependencies() {
1182 return android.Paths{android.PathForSource(ctx, jar)}
1183 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001184 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001185 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001186 return nil
1187 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001188 return android.Paths{jarPath.Path()}
1189}
1190
Paul Duffin47624362020-05-20 12:19:10 +01001191func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001192
Paul Duffin47624362020-05-20 12:19:10 +01001193 // Check any special cases for java_sdk_library.
1194 if !sdkVersion.specified() {
Paul Duffind1b3a922020-01-22 11:57:20 +00001195 if headerJars {
Paul Duffin47624362020-05-20 12:19:10 +01001196 return module.HeaderJars()
Paul Duffind1b3a922020-01-22 11:57:20 +00001197 } else {
Paul Duffin47624362020-05-20 12:19:10 +01001198 return module.ImplementationJars()
Sundong Ahn054b19a2018-10-19 13:46:09 +09001199 }
Paul Duffin47624362020-05-20 12:19:10 +01001200 } else if sdkVersion.kind == sdkPrivate {
1201 return module.HeaderJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +09001202 }
Paul Duffin47624362020-05-20 12:19:10 +01001203
Paul Duffina3fb67d2020-05-20 14:20:02 +01001204 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001205}
1206
Sundong Ahn241cd372018-07-13 16:16:44 +09001207// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001208func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1209 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1210}
1211
1212// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001213func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001214 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001215}
1216
Sundong Ahn80a87b32019-05-13 15:02:50 +09001217func (module *SdkLibrary) SetNoDist() {
1218 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1219}
1220
Colin Cross571cccf2019-02-04 11:22:08 -08001221var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1222
Jiyong Park82484c02018-04-23 21:41:26 +09001223func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001224 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001225 return &[]string{}
1226 }).(*[]string)
1227}
1228
Paul Duffin749f98f2019-12-30 17:23:46 +00001229func (module *SdkLibrary) getApiDir() string {
1230 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1231}
1232
Jiyong Parkc678ad32018-04-10 13:07:10 +09001233// For a java_sdk_library module, create internal modules for stubs, docs,
1234// runtime libs and xml file. If requested, the stubs and docs are created twice
1235// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001236func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1237 // If the module has been disabled then don't create any child modules.
1238 if !module.Enabled() {
1239 return
1240 }
1241
Paul Duffinc5d954a2020-05-16 18:54:24 +01001242 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001243 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001244 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001245 }
1246
Paul Duffin37e0b772019-12-30 17:20:10 +00001247 // If this builds against standard libraries (i.e. is not part of the core libraries)
1248 // then assume it provides both system and test apis. Otherwise, assume it does not and
1249 // also assume it does not contribute to the dist build.
1250 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1251 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001252 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001253 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1254
Inseob Kim8098faa2019-03-18 10:19:51 +09001255 missing_current_api := false
1256
Paul Duffin3a254982020-04-28 10:44:03 +01001257 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001258
Paul Duffin749f98f2019-12-30 17:23:46 +00001259 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001260 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001261 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001262 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001263 p := android.ExistentPathForSource(mctx, path)
1264 if !p.Valid() {
1265 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1266 missing_current_api = true
1267 }
1268 }
1269 }
1270
1271 if missing_current_api {
1272 script := "build/soong/scripts/gen-java-current-api-files.sh"
1273 p := android.ExistentPathForSource(mctx, script)
1274
1275 if !p.Valid() {
1276 panic(fmt.Sprintf("script file %s doesn't exist", script))
1277 }
1278
1279 mctx.ModuleErrorf("One or more current api files are missing. "+
1280 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001281 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001282 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001283 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001284 return
1285 }
1286
Paul Duffin3a254982020-04-28 10:44:03 +01001287 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001288 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001289 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001290
1291 // If the args needed to generate the stubs and API are the same then they
1292 // can be generated in a single invocation of metalava, otherwise they will
1293 // need separate invocations.
1294 if scope.createStubsSourceAndApiTogether {
1295 // Use the stubs source name for legacy reasons.
1296 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1297 } else {
1298 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1299
1300 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001301 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001302 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1303 }
1304
Paul Duffind1b3a922020-01-22 11:57:20 +00001305 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001306 }
1307
Paul Duffin43db9be2019-12-30 17:35:49 +00001308 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
1309 // for runtime
1310 module.createXmlFile(mctx)
1311
1312 // record java_sdk_library modules so that they are exported to make
1313 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1314 javaSdkLibrariesLock.Lock()
1315 defer javaSdkLibrariesLock.Unlock()
1316 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1317 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001318}
1319
1320func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001321 module.AddProperties(
1322 &module.sdkLibraryProperties,
Paul Duffinc5d954a2020-05-16 18:54:24 +01001323 &module.properties,
1324 &module.dexpreoptProperties,
1325 &module.deviceProperties,
1326 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001327 )
1328
Paul Duffinc5d954a2020-05-16 18:54:24 +01001329 module.properties.Installable = proptools.BoolPtr(true)
1330 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001331}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001332
Paul Duffin1a724e62020-05-08 13:44:43 +01001333// Defines how to name the individual component modules the sdk library creates.
1334type sdkLibraryComponentNamingScheme interface {
1335 stubsLibraryModuleName(scope *apiScope, baseName string) string
1336
1337 stubsSourceModuleName(scope *apiScope, baseName string) string
1338
1339 apiModuleName(scope *apiScope, baseName string) string
1340}
1341
1342type defaultNamingScheme struct {
1343}
1344
1345func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1346 return scope.stubsLibraryModuleName(baseName)
1347}
1348
1349func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1350 return scope.stubsSourceModuleName(baseName)
1351}
1352
1353func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1354 return scope.apiModuleName(baseName)
1355}
1356
1357var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1358
Paul Duffindef8a892020-05-08 15:36:30 +01001359type frameworkModulesNamingScheme struct {
1360}
1361
1362func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1363 suffix := scope.name
1364 if scope == apiScopeModuleLib {
1365 suffix = "module_libs_"
1366 }
1367 return suffix
1368}
1369
1370func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1371 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1372}
1373
1374func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1375 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1376}
1377
1378func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1379 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1380}
1381
1382var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1383
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001384// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1385// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1386// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1387// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1388// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001389func SdkLibraryFactory() android.Module {
1390 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001391
1392 // Initialize information common between source and prebuilt.
1393 module.initCommon(&module.ModuleBase)
1394
Inseob Kimc0907f12019-02-08 21:00:45 +09001395 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001396 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001397 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001398
1399 // Initialize the map from scope to scope specific properties.
1400 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1401 for _, scope := range allApiScopes {
1402 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1403 }
1404 module.scopeToProperties = scopeToProperties
1405
Paul Duffin344c4ee2020-04-29 23:35:13 +01001406 // Add the properties containing visibility rules so that they are checked.
1407 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1408 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1409
Paul Duffin1a724e62020-05-08 13:44:43 +01001410 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
1411 if module.initCommonAfterDefaultsApplied(ctx) {
1412 module.CreateInternalModules(ctx)
1413 }
1414 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001415 return module
1416}
Colin Cross79c7c262019-04-17 11:11:46 -07001417
1418//
1419// SDK library prebuilts
1420//
1421
Paul Duffin56d44902020-01-31 13:36:25 +00001422// Properties associated with each api scope.
1423type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001424 Jars []string `android:"path"`
1425
1426 Sdk_version *string
1427
Colin Cross79c7c262019-04-17 11:11:46 -07001428 // List of shared java libs that this module has dependencies to
1429 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001430
Paul Duffin5fb82132020-04-29 20:45:27 +01001431 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001432 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001433
1434 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001435 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001436
1437 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001438 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001439}
1440
Paul Duffin56d44902020-01-31 13:36:25 +00001441type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001442 // List of shared java libs, common to all scopes, that this module has
1443 // dependencies to
1444 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001445}
1446
Colin Cross79c7c262019-04-17 11:11:46 -07001447type sdkLibraryImport struct {
1448 android.ModuleBase
1449 android.DefaultableModuleBase
1450 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001451 android.ApexModuleBase
1452 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001453
1454 properties sdkLibraryImportProperties
1455
Paul Duffin6a2bd112020-04-07 19:27:04 +01001456 // Map from api scope to the scope specific property structure.
1457 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1458
Paul Duffin56d44902020-01-31 13:36:25 +00001459 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001460}
1461
1462var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1463
Paul Duffin6a2bd112020-04-07 19:27:04 +01001464// The type of a structure that contains a field of type sdkLibraryScopeProperties
1465// for each apiscope in allApiScopes, e.g. something like:
1466// struct {
1467// Public sdkLibraryScopeProperties
1468// System sdkLibraryScopeProperties
1469// ...
1470// }
1471var allScopeStructType = createAllScopePropertiesStructType()
1472
1473// Dynamically create a structure type for each apiscope in allApiScopes.
1474func createAllScopePropertiesStructType() reflect.Type {
1475 var fields []reflect.StructField
1476 for _, apiScope := range allApiScopes {
1477 field := reflect.StructField{
1478 Name: apiScope.fieldName,
1479 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1480 }
1481 fields = append(fields, field)
1482 }
1483
1484 return reflect.StructOf(fields)
1485}
1486
1487// Create an instance of the scope specific structure type and return a map
1488// from apiscope to a pointer to each scope specific field.
1489func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1490 allScopePropertiesPtr := reflect.New(allScopeStructType)
1491 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1492 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1493
1494 for _, apiScope := range allApiScopes {
1495 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1496 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1497 }
1498
1499 return allScopePropertiesPtr.Interface(), scopeProperties
1500}
1501
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001502// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001503func sdkLibraryImportFactory() android.Module {
1504 module := &sdkLibraryImport{}
1505
Paul Duffin6a2bd112020-04-07 19:27:04 +01001506 allScopeProperties, scopeToProperties := createPropertiesInstance()
1507 module.scopeProperties = scopeToProperties
1508 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001509
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001510 // Initialize information common between source and prebuilt.
1511 module.initCommon(&module.ModuleBase)
1512
Paul Duffin0bdcb272020-02-06 15:24:57 +00001513 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001514 android.InitApexModule(module)
1515 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001516 InitJavaModule(module, android.HostAndDeviceSupported)
1517
Paul Duffin1a724e62020-05-08 13:44:43 +01001518 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1519 if module.initCommonAfterDefaultsApplied(mctx) {
1520 module.createInternalModules(mctx)
1521 }
1522 })
Colin Cross79c7c262019-04-17 11:11:46 -07001523 return module
1524}
1525
1526func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1527 return &module.prebuilt
1528}
1529
1530func (module *sdkLibraryImport) Name() string {
1531 return module.prebuilt.Name(module.ModuleBase.Name())
1532}
1533
Paul Duffinbf735aa2020-05-08 15:01:19 +01001534func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001535
Paul Duffin50061512020-01-21 16:31:05 +00001536 // If the build is configured to use prebuilts then force this to be preferred.
1537 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1538 module.prebuilt.ForcePrefer()
1539 }
1540
Paul Duffin6a2bd112020-04-07 19:27:04 +01001541 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001542 if len(scopeProperties.Jars) == 0 {
1543 continue
1544 }
1545
Paul Duffinf6155722020-04-09 00:07:11 +01001546 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001547
Paul Duffin533f9c72020-05-20 16:18:00 +01001548 if len(scopeProperties.Stub_srcs) > 0 {
1549 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1550 }
Paul Duffin56d44902020-01-31 13:36:25 +00001551 }
Colin Cross79c7c262019-04-17 11:11:46 -07001552
1553 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1554 javaSdkLibrariesLock.Lock()
1555 defer javaSdkLibrariesLock.Unlock()
1556 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1557}
1558
Paul Duffinbf735aa2020-05-08 15:01:19 +01001559func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001560 // Creates a java import for the jar with ".stubs" suffix
1561 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001562 Name *string
1563 Sdk_version *string
1564 Libs []string
1565 Jars []string
1566 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001567 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001568 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001569 props.Sdk_version = scopeProperties.Sdk_version
1570 // Prepend any of the libs from the legacy public properties to the libs for each of the
1571 // scopes to avoid having to duplicate them in each scope.
1572 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1573 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001574
Paul Duffindd89a282020-05-13 16:08:09 +01001575 // The imports are preferred if the java_sdk_library_import is preferred.
1576 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf6155722020-04-09 00:07:11 +01001577 mctx.CreateModule(ImportFactory, &props)
1578}
1579
Paul Duffinbf735aa2020-05-08 15:01:19 +01001580func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001581 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001582 Name *string
1583 Srcs []string
1584 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001585 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001586 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001587 props.Srcs = scopeProperties.Stub_srcs
1588 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001589
1590 // The stubs source is preferred if the java_sdk_library_import is preferred.
1591 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001592}
1593
Colin Cross79c7c262019-04-17 11:11:46 -07001594func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001595 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001596 if len(scopeProperties.Jars) == 0 {
1597 continue
1598 }
1599
1600 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001601 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001602
1603 if len(scopeProperties.Stub_srcs) > 0 {
1604 // Add dependencies to the prebuilt stubs source library
1605 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1606 }
Paul Duffin56d44902020-01-31 13:36:25 +00001607 }
Colin Cross79c7c262019-04-17 11:11:46 -07001608}
1609
Paul Duffin46fdda82020-05-14 15:39:10 +01001610func (module *sdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
1611 return module.commonOutputFiles(tag)
1612}
1613
Colin Cross79c7c262019-04-17 11:11:46 -07001614func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001615 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001616 ctx.VisitDirectDeps(func(to android.Module) {
1617 tag := ctx.OtherModuleDependencyTag(to)
1618
Paul Duffin533f9c72020-05-20 16:18:00 +01001619 // Extract information from any of the scope specific dependencies.
1620 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1621 apiScope := scopeTag.apiScope
1622 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1623
1624 // Extract information from the dependency. The exact information extracted
1625 // is determined by the nature of the dependency which is determined by the tag.
1626 scopeTag.extractDepInfo(ctx, to, scopePaths)
Colin Cross79c7c262019-04-17 11:11:46 -07001627 }
1628 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001629
1630 // Populate the scope paths with information from the properties.
1631 for apiScope, scopeProperties := range module.scopeProperties {
1632 if len(scopeProperties.Jars) == 0 {
1633 continue
1634 }
1635
1636 paths := module.getScopePathsCreateIfNeeded(apiScope)
1637 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1638 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1639 }
Colin Cross79c7c262019-04-17 11:11:46 -07001640}
1641
Paul Duffin47624362020-05-20 12:19:10 +01001642func (module *sdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffina3fb67d2020-05-20 14:20:02 +01001643 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001644}
1645
Colin Cross79c7c262019-04-17 11:11:46 -07001646// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001647func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001648 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001649 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001650}
1651
1652// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001653func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001654 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001655 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001656}
Jiyong Parke3833882020-02-17 17:28:10 +09001657
1658//
1659// java_sdk_library_xml
1660//
1661type sdkLibraryXml struct {
1662 android.ModuleBase
1663 android.DefaultableModuleBase
1664 android.ApexModuleBase
1665
1666 properties sdkLibraryXmlProperties
1667
1668 outputFilePath android.OutputPath
1669 installDirPath android.InstallPath
1670}
1671
1672type sdkLibraryXmlProperties struct {
1673 // canonical name of the lib
1674 Lib_name *string
1675}
1676
1677// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1678// Not to be used directly by users. java_sdk_library internally uses this.
1679func sdkLibraryXmlFactory() android.Module {
1680 module := &sdkLibraryXml{}
1681
1682 module.AddProperties(&module.properties)
1683
1684 android.InitApexModule(module)
1685 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1686
1687 return module
1688}
1689
1690// from android.PrebuiltEtcModule
1691func (module *sdkLibraryXml) SubDir() string {
1692 return "permissions"
1693}
1694
1695// from android.PrebuiltEtcModule
1696func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1697 return module.outputFilePath
1698}
1699
1700// from android.ApexModule
1701func (module *sdkLibraryXml) AvailableFor(what string) bool {
1702 return true
1703}
1704
1705func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1706 // do nothing
1707}
1708
1709// File path to the runtime implementation library
1710func (module *sdkLibraryXml) implPath() string {
1711 implName := proptools.String(module.properties.Lib_name)
1712 if apexName := module.ApexName(); apexName != "" {
1713 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1714 // In most cases, this works fine. But when apex_name is set or override_apex is used
1715 // this can be wrong.
1716 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1717 }
1718 partition := "system"
1719 if module.SocSpecific() {
1720 partition = "vendor"
1721 } else if module.DeviceSpecific() {
1722 partition = "odm"
1723 } else if module.ProductSpecific() {
1724 partition = "product"
1725 } else if module.SystemExtSpecific() {
1726 partition = "system_ext"
1727 }
1728 return "/" + partition + "/framework/" + implName + ".jar"
1729}
1730
1731func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1732 libName := proptools.String(module.properties.Lib_name)
1733 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1734
1735 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1736 rule := android.NewRuleBuilder()
1737 rule.Command().
1738 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1739 Output(module.outputFilePath)
1740
1741 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1742
1743 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1744}
1745
1746func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1747 if !module.IsForPlatform() {
1748 return []android.AndroidMkEntries{android.AndroidMkEntries{
1749 Disabled: true,
1750 }}
1751 }
1752
1753 return []android.AndroidMkEntries{android.AndroidMkEntries{
1754 Class: "ETC",
1755 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1756 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1757 func(entries *android.AndroidMkEntries) {
1758 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1759 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1760 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1761 },
1762 },
1763 }}
1764}
Paul Duffin61871622020-02-10 13:37:10 +00001765
1766type sdkLibrarySdkMemberType struct {
1767 android.SdkMemberTypeBase
1768}
1769
1770func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1771 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1772}
1773
1774func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1775 _, ok := module.(*SdkLibrary)
1776 return ok
1777}
1778
1779func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1780 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1781}
1782
1783func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1784 return &sdkLibrarySdkMemberProperties{}
1785}
1786
1787type sdkLibrarySdkMemberProperties struct {
1788 android.SdkMemberPropertiesBase
1789
1790 // Scope to per scope properties.
1791 Scopes map[*apiScope]scopeProperties
1792
1793 // Additional libraries that the exported stubs libraries depend upon.
1794 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001795
1796 // The Java stubs source files.
1797 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01001798
1799 // The naming scheme.
1800 Naming_scheme *string
Paul Duffin61871622020-02-10 13:37:10 +00001801}
1802
1803type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01001804 Jars android.Paths
1805 StubsSrcJar android.Path
1806 CurrentApiFile android.Path
1807 RemovedApiFile android.Path
1808 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00001809}
1810
1811func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1812 sdk := variant.(*SdkLibrary)
1813
1814 s.Scopes = make(map[*apiScope]scopeProperties)
1815 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01001816 paths := sdk.findScopePaths(apiScope)
1817 if paths == nil {
1818 continue
1819 }
1820
Paul Duffin61871622020-02-10 13:37:10 +00001821 jars := paths.stubsImplPath
1822 if len(jars) > 0 {
1823 properties := scopeProperties{}
1824 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01001825 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01001826 properties.StubsSrcJar = paths.stubsSrcJar.Path()
1827 properties.CurrentApiFile = paths.currentApiFilePath.Path()
1828 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffin61871622020-02-10 13:37:10 +00001829 s.Scopes[apiScope] = properties
1830 }
1831 }
1832
1833 s.Libs = sdk.properties.Libs
Paul Duffinf8e08b22020-05-13 16:54:55 +01001834 s.Naming_scheme = sdk.commonProperties.Naming_scheme
Paul Duffin61871622020-02-10 13:37:10 +00001835}
1836
1837func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01001838 if s.Naming_scheme != nil {
1839 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
1840 }
1841
Paul Duffin61871622020-02-10 13:37:10 +00001842 for _, apiScope := range allApiScopes {
1843 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01001844 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00001845
Paul Duffinf488ef22020-04-09 00:10:17 +01001846 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1847
Paul Duffin61871622020-02-10 13:37:10 +00001848 var jars []string
1849 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01001850 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00001851 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1852 jars = append(jars, dest)
1853 }
1854 scopeSet.AddProperty("jars", jars)
1855
Paul Duffinf488ef22020-04-09 00:10:17 +01001856 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1857 // the source files are also unpacked.
1858 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1859 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1860 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1861
Paul Duffin75dcc802020-04-09 01:08:11 +01001862 if properties.CurrentApiFile != nil {
1863 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1864 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1865 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1866 }
1867
1868 if properties.RemovedApiFile != nil {
1869 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1870 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1871 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1872 }
1873
Paul Duffin61871622020-02-10 13:37:10 +00001874 if properties.SdkVersion != "" {
1875 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1876 }
1877 }
1878 }
1879
1880 if len(s.Libs) > 0 {
1881 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1882 }
1883}