blob: 7d973c4fbf55dda5f06681526d98c91ec2b26cdc [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,
Paul Duffin5a757b12020-06-02 13:00:08 +0100267 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100268 //
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 Duffin5a757b12020-06-02 13:00:08 +0100283 apiScopeSystemServer = initApiScope(&apiScope{
284 name: "system-server",
285 extends: apiScopePublic,
286 // The system-server scope is disabled by default in legacy mode.
287 //
288 // Enabling this would break existing usages.
289 legacyEnabledStatus: func(module *SdkLibrary) bool {
290 return false
291 },
292 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
293 return &module.sdkLibraryProperties.System_server
294 },
295 apiFilePrefix: "system-server-",
296 moduleSuffix: ".system_server",
297 sdkVersion: "system_server_current",
298 droidstubsArgs: []string{
299 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.SYSTEM_SERVER\\) ",
300 "--hide-annotation android.annotation.Hide",
301 // com.android.* classes are okay in this interface"
302 "--hide InternalClasses",
303 },
304 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000305 allApiScopes = apiScopes{
306 apiScopePublic,
307 apiScopeSystem,
308 apiScopeTest,
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100309 apiScopeModuleLib,
Paul Duffin5a757b12020-06-02 13:00:08 +0100310 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000311 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900312)
313
Jiyong Park82484c02018-04-23 21:41:26 +0900314var (
315 javaSdkLibrariesLock sync.Mutex
316)
317
Jiyong Parkc678ad32018-04-10 13:07:10 +0900318// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900319// 1) disallowing linking to the runtime shared lib
320// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900321
322func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000323 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900324
Jiyong Park82484c02018-04-23 21:41:26 +0900325 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
326 javaSdkLibraries := javaSdkLibraries(ctx.Config())
327 sort.Strings(*javaSdkLibraries)
328 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
329 })
Paul Duffin61871622020-02-10 13:37:10 +0000330
331 // Register sdk member types.
332 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
333 android.SdkMemberTypeBase{
334 PropertyName: "java_sdk_libs",
335 SupportsSdk: true,
336 },
337 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900338}
339
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000340func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
341 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
342 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
343}
344
Paul Duffin3a254982020-04-28 10:44:03 +0100345// Properties associated with each api scope.
346type ApiScopeProperties struct {
347 // Indicates whether the api surface is generated.
348 //
349 // If this is set for any scope then all scopes must explicitly specify if they
350 // are enabled. This is to prevent new usages from depending on legacy behavior.
351 //
352 // Otherwise, if this is not set for any scope then the default behavior is
353 // scope specific so please refer to the scope specific property documentation.
354 Enabled *bool
Paul Duffin080f5ee2020-05-12 11:50:28 +0100355
356 // The sdk_version to use for building the stubs.
357 //
358 // If not specified then it will use an sdk_version determined as follows:
359 // 1) If the sdk_version specified on the java_sdk_library is none then this
360 // will be none. This is used for java_sdk_library instances that are used
361 // to create stubs that contribute to the core_current sdk version.
362 // 2) Otherwise, it is assumed that this library extends but does not contribute
363 // directly to a specific sdk_version and so this uses the sdk_version appropriate
364 // for the api scope. e.g. public will use sdk_version: current, system will use
365 // sdk_version: system_current, etc.
366 //
367 // This does not affect the sdk_version used for either generating the stubs source
368 // or the API file. They both have to use the same sdk_version as is used for
369 // compiling the implementation library.
370 Sdk_version *string
Paul Duffin3a254982020-04-28 10:44:03 +0100371}
372
Jiyong Parkc678ad32018-04-10 13:07:10 +0900373type sdkLibraryProperties struct {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100374 // Visibility for impl library module. If not specified then defaults to the
375 // visibility property.
376 Impl_library_visibility []string
377
Paul Duffin344c4ee2020-04-29 23:35:13 +0100378 // Visibility for stubs library modules. If not specified then defaults to the
379 // visibility property.
380 Stubs_library_visibility []string
381
382 // Visibility for stubs source modules. If not specified then defaults to the
383 // visibility property.
384 Stubs_source_visibility []string
385
Sundong Ahnf043cf62018-06-25 16:04:37 +0900386 // List of Java libraries that will be in the classpath when building stubs
387 Stub_only_libs []string `android:"arch_variant"`
388
Paul Duffin7a586d32019-12-30 17:09:34 +0000389 // list of package names that will be documented and publicized as API.
390 // This allows the API to be restricted to a subset of the source files provided.
391 // If this is unspecified then all the source files will be treated as being part
392 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393 Api_packages []string
394
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900395 // list of package names that must be hidden from the API
396 Hidden_api_packages []string
397
Paul Duffin749f98f2019-12-30 17:23:46 +0000398 // the relative path to the directory containing the api specification files.
399 // Defaults to "api".
400 Api_dir *string
401
Paul Duffind11e78e2020-05-15 20:37:11 +0100402 // Determines whether a runtime implementation library is built; defaults to false.
403 //
404 // If true then it also prevents the module from being used as a shared module, i.e.
405 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000406 Api_only *bool
407
Paul Duffin11512472019-02-11 15:55:17 +0000408 // local files that are used within user customized droiddoc options.
409 Droiddoc_option_files []string
410
411 // additional droiddoc options
412 // Available variables for substitution:
413 //
414 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900415 Droiddoc_options []string
416
Paul Duffin2ce1e812020-05-20 19:35:27 +0100417 // is set to true, Metalava will allow framework SDK to contain annotations.
418 Annotations_enabled *bool
419
Sundong Ahn054b19a2018-10-19 13:46:09 +0900420 // a list of top-level directories containing files to merge qualifier annotations
421 // (i.e. those intended to be included in the stubs written) from.
422 Merge_annotations_dirs []string
423
424 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
425 Merge_inclusion_annotations_dirs []string
426
427 // If set to true, the path of dist files is apistubs/core. Defaults to false.
428 Core_lib *bool
429
Sundong Ahn80a87b32019-05-13 15:02:50 +0900430 // don't create dist rules.
431 No_dist *bool `blueprint:"mutated"`
432
Paul Duffin3a254982020-04-28 10:44:03 +0100433 // indicates whether system and test apis should be generated.
434 Generate_system_and_test_apis bool `blueprint:"mutated"`
435
436 // The properties specific to the public api scope
437 //
438 // Unless explicitly specified by using public.enabled the public api scope is
439 // enabled by default in both legacy and non-legacy mode.
440 Public ApiScopeProperties
441
442 // The properties specific to the system api scope
443 //
444 // In legacy mode the system api scope is enabled by default when sdk_version
445 // is set to something other than "none".
446 //
447 // In non-legacy mode the system api scope is disabled by default.
448 System ApiScopeProperties
449
450 // The properties specific to the test api scope
451 //
452 // In legacy mode the test api scope is enabled by default when sdk_version
453 // is set to something other than "none".
454 //
455 // In non-legacy mode the test api scope is disabled by default.
456 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000457
Paul Duffin5a757b12020-06-02 13:00:08 +0100458 // The properties specific to the module-lib api scope
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100459 //
Paul Duffin5a757b12020-06-02 13:00:08 +0100460 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100461 // disabled by default.
462 Module_lib ApiScopeProperties
463
Paul Duffin5a757b12020-06-02 13:00:08 +0100464 // The properties specific to the system-server api scope
465 //
466 // Unless explicitly specified by using test.enabled the module-lib api scope is
467 // disabled by default.
468 System_server ApiScopeProperties
469
Jiyong Park27fc4142020-05-28 00:19:53 +0900470 // Determines if the stubs are preferred over the implementation library
471 // for linking, even when the client doesn't specify sdk_version. When this
472 // is set to true, such clients are provided with the widest API surface that
473 // this lib provides. Note however that this option doesn't affect the clients
474 // that are in the same APEX as this library. In that case, the clients are
475 // always linked with the implementation library. Default is false.
476 Default_to_stubs *bool
477
Paul Duffin8986cc92020-05-10 19:32:20 +0100478 // Properties related to api linting.
479 Api_lint struct {
480 // Enable api linting.
481 Enabled *bool
482 }
483
Jiyong Parkc678ad32018-04-10 13:07:10 +0900484 // TODO: determines whether to create HTML doc or not
485 //Html_doc *bool
486}
487
Paul Duffin533f9c72020-05-20 16:18:00 +0100488// Paths to outputs from java_sdk_library and java_sdk_library_import.
489//
490// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
491// OptionalPaths are always set by java_sdk_library but may not be set by
492// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000493type scopePaths struct {
Paul Duffin533f9c72020-05-20 16:18:00 +0100494 // The path (represented as Paths for convenience when returning) to the stubs header jar.
495 //
496 // That is the jar that is created by turbine.
497 stubsHeaderPath android.Paths
498
499 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
500 //
501 // This is not the implementation jar, it still only contains stubs.
502 stubsImplPath android.Paths
503
504 // The API specification file, e.g. system_current.txt.
505 currentApiFilePath android.OptionalPath
506
507 // The specification of API elements removed since the last release.
508 removedApiFilePath android.OptionalPath
509
510 // The stubs source jar.
511 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000512}
513
Paul Duffin5fb82132020-04-29 20:45:27 +0100514func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
515 if lib, ok := dep.(Dependency); ok {
516 paths.stubsHeaderPath = lib.HeaderJars()
517 paths.stubsImplPath = lib.ImplementationJars()
518 return nil
519 } else {
520 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
521 }
522}
523
Paul Duffina377e4c2020-04-29 13:30:54 +0100524func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
525 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
526 action(apiStubsProvider)
Paul Duffin5fb82132020-04-29 20:45:27 +0100527 return nil
528 } else {
529 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
530 }
531}
532
Paul Duffin533f9c72020-05-20 16:18:00 +0100533func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
534 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
535 action(apiStubsProvider)
536 return nil
537 } else {
538 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
539 }
540}
541
Paul Duffina377e4c2020-04-29 13:30:54 +0100542func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin533f9c72020-05-20 16:18:00 +0100543 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
544 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffina377e4c2020-04-29 13:30:54 +0100545}
546
547func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
548 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
549 paths.extractApiInfoFromApiStubsProvider(provider)
550 })
551}
552
Paul Duffin533f9c72020-05-20 16:18:00 +0100553func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
554 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffina377e4c2020-04-29 13:30:54 +0100555}
556
557func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin533f9c72020-05-20 16:18:00 +0100558 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffina377e4c2020-04-29 13:30:54 +0100559 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
560 })
561}
562
563func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
564 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
565 paths.extractApiInfoFromApiStubsProvider(provider)
566 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
567 })
568}
569
570type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1a724e62020-05-08 13:44:43 +0100571 // The naming scheme to use for the components that this module creates.
572 //
Paul Duffindef8a892020-05-08 15:36:30 +0100573 // If not specified then it defaults to "default". The other allowable value is
574 // "framework-modules" which matches the scheme currently used by framework modules
575 // for the equivalent components represented as separate Soong modules.
Paul Duffin1a724e62020-05-08 13:44:43 +0100576 //
577 // This is a temporary mechanism to simplify conversion from separate modules for each
578 // component that follow a different naming pattern to the default one.
579 //
580 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffina377e4c2020-04-29 13:30:54 +0100581 Naming_scheme *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100582
583 // Specifies whether this module can be used as an Android shared library; defaults
584 // to true.
585 //
586 // An Android shared library is one that can be referenced in a <uses-library> element
587 // in an AndroidManifest.xml.
588 Shared_library *bool
Paul Duffina377e4c2020-04-29 13:30:54 +0100589}
590
Paul Duffin56d44902020-01-31 13:36:25 +0000591// Common code between sdk library and sdk library import
592type commonToSdkLibraryAndImport struct {
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100593 moduleBase *android.ModuleBase
594
Paul Duffin56d44902020-01-31 13:36:25 +0000595 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100596
597 namingScheme sdkLibraryComponentNamingScheme
598
Paul Duffind11e78e2020-05-15 20:37:11 +0100599 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin64e61992020-05-15 10:20:31 +0100600
601 // Functionality related to this being used as a component of a java_sdk_library.
602 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000603}
604
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100605func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
606 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100607
Paul Duffind11e78e2020-05-15 20:37:11 +0100608 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin64e61992020-05-15 10:20:31 +0100609
610 // Initialize this as an sdk library component.
611 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1a724e62020-05-08 13:44:43 +0100612}
613
614func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffind11e78e2020-05-15 20:37:11 +0100615 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1a724e62020-05-08 13:44:43 +0100616 switch schemeProperty {
617 case "default":
618 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100619 case "framework-modules":
620 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100621 default:
622 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
623 return false
624 }
625
Paul Duffind11e78e2020-05-15 20:37:11 +0100626 // Only track this sdk library if this can be used as a shared library.
627 if c.sharedLibrary() {
628 // Use the name specified in the module definition as the owner.
629 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
630 }
Paul Duffin64e61992020-05-15 10:20:31 +0100631
Paul Duffin1a724e62020-05-08 13:44:43 +0100632 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100633}
634
Paul Duffinf642a312020-06-12 17:46:39 +0100635// Module name of the runtime implementation library
636func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
637 return c.moduleBase.BaseModuleName() + ".impl"
638}
639
640// Module name of the XML file for the lib
641func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
642 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
643}
644
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100645// Name of the java_library module that compiles the stubs source.
646func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100647 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100648}
649
650// Name of the droidstubs module that generates the stubs source and may also
651// generate/check the API.
652func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100653 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100654}
655
656// Name of the droidstubs module that generates/checks the API. Only used if it
657// requires different arts to the stubs source generating module.
658func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100659 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100660}
661
Paul Duffin46fdda82020-05-14 15:39:10 +0100662// The component names for different outputs of the java_sdk_library.
663//
664// They are similar to the names used for the child modules it creates
665const (
666 stubsSourceComponentName = "stubs.source"
667
668 apiTxtComponentName = "api.txt"
669
670 removedApiTxtComponentName = "removed-api.txt"
671)
672
673// A regular expression to match tags that reference a specific stubs component.
674//
675// It will only match if given a valid scope and a valid component. It is verfy strict
676// to ensure it does not accidentally match a similar looking tag that should be processed
677// by the embedded Library.
678var tagSplitter = func() *regexp.Regexp {
679 // Given a list of literal string items returns a regular expression that will
680 // match any one of the items.
681 choice := func(items ...string) string {
682 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
683 }
684
685 // Regular expression to match one of the scopes.
686 scopesRegexp := choice(allScopeNames...)
687
688 // Regular expression to match one of the components.
689 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
690
691 // Regular expression to match any combination of one scope and one component.
692 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
693}()
694
695// For OutputFileProducer interface
696//
697// .<scope>.stubs.source
698// .<scope>.api.txt
699// .<scope>.removed-api.txt
700func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
701 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
702 scopeName := groups[1]
703 component := groups[2]
704
705 if scope, ok := scopeByName[scopeName]; ok {
706 paths := c.findScopePaths(scope)
707 if paths == nil {
708 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
709 }
710
711 switch component {
712 case stubsSourceComponentName:
713 if paths.stubsSrcJar.Valid() {
714 return android.Paths{paths.stubsSrcJar.Path()}, nil
715 }
716
717 case apiTxtComponentName:
718 if paths.currentApiFilePath.Valid() {
719 return android.Paths{paths.currentApiFilePath.Path()}, nil
720 }
721
722 case removedApiTxtComponentName:
723 if paths.removedApiFilePath.Valid() {
724 return android.Paths{paths.removedApiFilePath.Path()}, nil
725 }
726 }
727
728 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
729 } else {
730 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
731 }
732
733 } else {
734 return nil, nil
735 }
736}
737
Paul Duffin5ae30792020-05-20 11:52:25 +0100738func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000739 if c.scopePaths == nil {
740 c.scopePaths = make(map[*apiScope]*scopePaths)
741 }
742 paths := c.scopePaths[scope]
743 if paths == nil {
744 paths = &scopePaths{}
745 c.scopePaths[scope] = paths
746 }
747
748 return paths
749}
750
Paul Duffin5ae30792020-05-20 11:52:25 +0100751func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
752 if c.scopePaths == nil {
753 return nil
754 }
755
756 return c.scopePaths[scope]
757}
758
759// If this does not support the requested api scope then find the closest available
760// scope it does support. Returns nil if no such scope is available.
761func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
762 for s := scope; s != nil; s = s.extends {
763 if paths := c.findScopePaths(s); paths != nil {
764 return paths
765 }
766 }
767
768 // This should never happen outside tests as public should be the base scope for every
769 // scope and is enabled by default.
770 return nil
771}
772
Paul Duffina3fb67d2020-05-20 14:20:02 +0100773func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-05-20 12:19:10 +0100774
775 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
776 if sdkVersion.version.isNumbered() {
777 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
778 }
779
780 var apiScope *apiScope
781 switch sdkVersion.kind {
782 case sdkSystem:
783 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100784 case sdkModule:
785 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100786 case sdkTest:
787 apiScope = apiScopeTest
Paul Duffin5a757b12020-06-02 13:00:08 +0100788 case sdkSystemServer:
789 apiScope = apiScopeSystemServer
Paul Duffin47624362020-05-20 12:19:10 +0100790 default:
791 apiScope = apiScopePublic
792 }
793
Paul Duffin5ae30792020-05-20 11:52:25 +0100794 paths := c.findClosestScopePath(apiScope)
795 if paths == nil {
796 var scopes []string
797 for _, s := range allApiScopes {
798 if c.findScopePaths(s) != nil {
799 scopes = append(scopes, s.name)
800 }
801 }
802 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
803 return nil
804 }
805
Paul Duffina3fb67d2020-05-20 14:20:02 +0100806 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100807}
808
Paul Duffin64e61992020-05-15 10:20:31 +0100809func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
810 componentProps := &struct {
811 SdkLibraryToImplicitlyTrack *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100812 }{}
813
814 if c.sharedLibrary() {
Paul Duffin64e61992020-05-15 10:20:31 +0100815 // Mark the stubs library as being components of this java_sdk_library so that
816 // any app that includes code which depends (directly or indirectly) on the stubs
817 // library will have the appropriate <uses-library> invocation inserted into its
818 // manifest if necessary.
Paul Duffind11e78e2020-05-15 20:37:11 +0100819 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin64e61992020-05-15 10:20:31 +0100820 }
821
822 return componentProps
823}
824
Paul Duffind11e78e2020-05-15 20:37:11 +0100825// Check if this can be used as a shared library.
826func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
827 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
828}
829
Paul Duffin64e61992020-05-15 10:20:31 +0100830// Properties related to the use of a module as an component of a java_sdk_library.
831type SdkLibraryComponentProperties struct {
832
833 // The name of the java_sdk_library/_import to add to a <uses-library> entry
834 // in the AndroidManifest.xml of any Android app that includes code that references
835 // this module. If not set then no java_sdk_library/_import is tracked.
836 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
837}
838
839// Structure to be embedded in a module struct that needs to support the
840// SdkLibraryComponentDependency interface.
841type EmbeddableSdkLibraryComponent struct {
842 sdkLibraryComponentProperties SdkLibraryComponentProperties
843}
844
845func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
846 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
847}
848
849// to satisfy SdkLibraryComponentDependency
850func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
851 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
852 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
853 }
854 return nil
855}
856
857// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
858// (including the java_sdk_library) itself.
859type SdkLibraryComponentDependency interface {
860 // The optional name of the sdk library that should be implicitly added to the
861 // AndroidManifest of an app that contains code which references the sdk library.
862 //
863 // Returns an array containing 0 or 1 items rather than a *string to make it easier
864 // to append this to the list of exported sdk libraries.
865 OptionalImplicitSdkLibrary() []string
866}
867
868// Make sure that all the module types that are components of java_sdk_library/_import
869// and which can be referenced (directly or indirectly) from an android app implement
870// the SdkLibraryComponentDependency interface.
871var _ SdkLibraryComponentDependency = (*Library)(nil)
872var _ SdkLibraryComponentDependency = (*Import)(nil)
873var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffinf642a312020-06-12 17:46:39 +0100874var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin64e61992020-05-15 10:20:31 +0100875
876// Provides access to sdk_version related header and implentation jars.
877type SdkLibraryDependency interface {
878 SdkLibraryComponentDependency
879
880 // Get the header jars appropriate for the supplied sdk_version.
881 //
882 // These are turbine generated jars so they only change if the externals of the
883 // class changes but it does not contain and implementation or JavaDoc.
884 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
885
886 // Get the implementation jars appropriate for the supplied sdk version.
887 //
888 // These are either the implementation jar for the whole sdk library or the implementation
889 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
890 // they are identical to the corresponding header jars.
891 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
892}
893
Inseob Kimc0907f12019-02-08 21:00:45 +0900894type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900895 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900896
Sundong Ahn054b19a2018-10-19 13:46:09 +0900897 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900898
Paul Duffin3a254982020-04-28 10:44:03 +0100899 // Map from api scope to the scope specific property structure.
900 scopeToProperties map[*apiScope]*ApiScopeProperties
901
Paul Duffin56d44902020-01-31 13:36:25 +0000902 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900903}
904
Inseob Kimc0907f12019-02-08 21:00:45 +0900905var _ Dependency = (*SdkLibrary)(nil)
906var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800907
Paul Duffin3a254982020-04-28 10:44:03 +0100908func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
909 return module.sdkLibraryProperties.Generate_system_and_test_apis
910}
911
912func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
913 // Check to see if any scopes have been explicitly enabled. If any have then all
914 // must be.
915 anyScopesExplicitlyEnabled := false
916 for _, scope := range allApiScopes {
917 scopeProperties := module.scopeToProperties[scope]
918 if scopeProperties.Enabled != nil {
919 anyScopesExplicitlyEnabled = true
920 break
921 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000922 }
Paul Duffin3a254982020-04-28 10:44:03 +0100923
924 var generatedScopes apiScopes
925 enabledScopes := make(map[*apiScope]struct{})
926 for _, scope := range allApiScopes {
927 scopeProperties := module.scopeToProperties[scope]
928 // If any scopes are explicitly enabled then ignore the legacy enabled status.
929 // This is to ensure that any new usages of this module type do not rely on legacy
930 // behaviour.
931 defaultEnabledStatus := false
932 if anyScopesExplicitlyEnabled {
933 defaultEnabledStatus = scope.defaultEnabledStatus
934 } else {
935 defaultEnabledStatus = scope.legacyEnabledStatus(module)
936 }
937 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
938 if enabled {
939 enabledScopes[scope] = struct{}{}
940 generatedScopes = append(generatedScopes, scope)
941 }
942 }
943
944 // Now check to make sure that any scope that is extended by an enabled scope is also
945 // enabled.
946 for _, scope := range allApiScopes {
947 if _, ok := enabledScopes[scope]; ok {
948 extends := scope.extends
949 if extends != nil {
950 if _, ok := enabledScopes[extends]; !ok {
951 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
952 }
953 }
954 }
955 }
956
957 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000958}
959
Paul Duffinf642a312020-06-12 17:46:39 +0100960type sdkLibraryComponentTag struct {
961 blueprint.BaseDependencyTag
962 name string
963}
964
965// Mark this tag so dependencies that use it are excluded from visibility enforcement.
966func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
967
968var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000969
Jiyong Parke3833882020-02-17 17:28:10 +0900970func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffinf642a312020-06-12 17:46:39 +0100971 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900972 return dt == xmlPermissionsFileTag
973 }
974 return false
975}
976
Paul Duffinf642a312020-06-12 17:46:39 +0100977var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin9d582cc2020-05-16 15:52:12 +0100978
Inseob Kimc0907f12019-02-08 21:00:45 +0900979func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100980 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000981 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100982 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000983
Paul Duffina377e4c2020-04-29 13:30:54 +0100984 // If the stubs source and API cannot be generated together then add an additional dependency on
985 // the API module.
986 if apiScope.createStubsSourceAndApiTogether {
987 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100988 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100989 } else {
990 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100991 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
992 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100993 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900994 }
995
Paul Duffind11e78e2020-05-15 20:37:11 +0100996 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100997 // Add dependency to the rule for generating the implementation library.
998 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
999
Paul Duffind11e78e2020-05-15 20:37:11 +01001000 if module.sharedLibrary() {
1001 // Add dependency to the rule for generating the xml permissions file
Paul Duffinf642a312020-06-12 17:46:39 +01001002 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffind11e78e2020-05-15 20:37:11 +01001003 }
Paul Duffine74ac732020-02-06 13:51:46 +00001004
Paul Duffind11e78e2020-05-15 20:37:11 +01001005 // Only add the deps for the library if it is actually going to be built.
1006 module.Library.deps(ctx)
1007 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001008}
1009
Paul Duffin46fdda82020-05-14 15:39:10 +01001010func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1011 paths, err := module.commonOutputFiles(tag)
1012 if paths == nil && err == nil {
1013 return module.Library.OutputFiles(tag)
1014 } else {
1015 return paths, err
1016 }
1017}
1018
Inseob Kimc0907f12019-02-08 21:00:45 +09001019func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001020 // Only build an implementation library if required.
1021 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001022 module.Library.GenerateAndroidBuildActions(ctx)
1023 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001024
Sundong Ahn57368eb2018-07-06 11:20:23 +09001025 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001026 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001027 // the recorded paths will be returned depending on the link type of the caller.
1028 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001029 tag := ctx.OtherModuleDependencyTag(to)
1030
Paul Duffin5fb82132020-04-29 20:45:27 +01001031 // Extract information from any of the scope specific dependencies.
1032 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1033 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +01001034 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +01001035
1036 // Extract information from the dependency. The exact information extracted
1037 // is determined by the nature of the dependency which is determined by the tag.
1038 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001039 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001040 })
1041}
1042
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001043func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffind11e78e2020-05-15 20:37:11 +01001044 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001045 return nil
1046 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001047 entriesList := module.Library.AndroidMkEntries()
1048 entries := &entriesList[0]
Paul Duffinf642a312020-06-12 17:46:39 +01001049 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001050 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001051}
1052
Anton Hansson6bb88102020-03-27 19:43:19 +00001053// The dist path of the stub artifacts
1054func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1055 if module.ModuleBase.Owner() != "" {
1056 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1057 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1058 return path.Join("apistubs", "core", apiScope.name)
1059 } else {
1060 return path.Join("apistubs", "android", apiScope.name)
1061 }
1062}
1063
Paul Duffin12ceb462019-12-24 20:31:31 +00001064// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +01001065func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +01001066 scopeProperties := module.scopeToProperties[apiScope]
1067 if scopeProperties.Sdk_version != nil {
1068 return proptools.String(scopeProperties.Sdk_version)
1069 }
1070
Paul Duffin12ceb462019-12-24 20:31:31 +00001071 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1072 if sdkDep.hasStandardLibs() {
1073 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001074 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001075 } else {
1076 // Otherwise, use no system module.
1077 return "none"
1078 }
1079}
1080
Paul Duffind1b3a922020-01-22 11:57:20 +00001081func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1082 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001083}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001084
Paul Duffind1b3a922020-01-22 11:57:20 +00001085func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1086 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001087}
1088
Paul Duffin9d582cc2020-05-16 15:52:12 +01001089// Creates the implementation java library
1090func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffinc4422102020-06-24 16:22:38 +01001091
1092 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1093
Paul Duffin9d582cc2020-05-16 15:52:12 +01001094 props := struct {
Paul Duffinc4422102020-06-24 16:22:38 +01001095 Name *string
1096 Visibility []string
1097 Instrument bool
1098 ConfigurationName *string
Paul Duffin9d582cc2020-05-16 15:52:12 +01001099 }{
1100 Name: proptools.StringPtr(module.implLibraryModuleName()),
1101 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
Paul Duffin49d3a522020-06-18 21:09:55 +01001102 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1103 Instrument: true,
Paul Duffinc4422102020-06-24 16:22:38 +01001104
1105 // Make the created library behave as if it had the same name as this module.
1106 ConfigurationName: moduleNamePtr,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001107 }
1108
1109 properties := []interface{}{
1110 &module.properties,
1111 &module.protoProperties,
1112 &module.deviceProperties,
Liz Kammer7727edc2020-07-09 15:16:41 -07001113 &module.dexProperties,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001114 &module.dexpreoptProperties,
Colin Cross1e28e3c2020-06-02 20:09:13 -07001115 &module.linter.properties,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001116 &props,
1117 module.sdkComponentPropertiesForChildLibrary(),
1118 }
1119 mctx.CreateModule(LibraryFactory, properties...)
1120}
1121
Jiyong Parkc678ad32018-04-10 13:07:10 +09001122// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001123func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001124 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001125 Name *string
1126 Visibility []string
1127 Srcs []string
1128 Installable *bool
1129 Sdk_version *string
1130 System_modules *string
1131 Patch_module *string
1132 Libs []string
1133 Compile_dex *bool
1134 Java_version *string
1135 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001136 Pdk struct {
1137 Enabled *bool
1138 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001139 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001140 Openjdk9 struct {
1141 Srcs []string
1142 Javacflags []string
1143 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001144 Dist struct {
1145 Targets []string
1146 Dest *string
1147 Dir *string
1148 Tag *string
1149 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001150 }{}
1151
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001152 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +01001153
1154 // If stubs_library_visibility is not set then the created module will use the
1155 // visibility of this module.
1156 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1157 props.Visibility = visibility
1158
Jiyong Parkc678ad32018-04-10 13:07:10 +09001159 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001160 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001161 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001162 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001163 props.System_modules = module.deviceProperties.System_modules
1164 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001165 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001166 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +01001167 // The stub-annotations library contains special versions of the annotations
1168 // with CLASS retention policy, so that they're kept.
1169 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1170 props.Libs = append(props.Libs, "stub-annotations")
1171 }
Jiyong Park82484c02018-04-23 21:41:26 +09001172 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001173 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1174 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hanssoncf4dd4c2020-05-21 09:21:57 +01001175 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1176 // interop with older developer tools that don't support 1.9.
1177 props.Java_version = proptools.StringPtr("1.8")
Liz Kammer7727edc2020-07-09 15:16:41 -07001178 if module.dexProperties.Compile_dex != nil {
1179 props.Compile_dex = module.dexProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001180 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001181
Anton Hansson6bb88102020-03-27 19:43:19 +00001182 // Dist the class jar artifact for sdk builds.
1183 if !Bool(module.sdkLibraryProperties.No_dist) {
1184 props.Dist.Targets = []string{"sdk", "win_sdk"}
1185 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1186 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1187 props.Dist.Tag = proptools.StringPtr(".jar")
1188 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001189
Paul Duffin64e61992020-05-15 10:20:31 +01001190 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001191}
1192
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001193// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001194// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +01001195func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001196 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001197 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001198 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001199 Srcs []string
1200 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001201 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001202 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001203 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001204 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001205 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001206 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +01001207 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001208 Merge_annotations_dirs []string
1209 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001210 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001211 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001212 Current ApiToCheck
1213 Last_released ApiToCheck
1214 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001215
1216 Api_lint struct {
1217 Enabled *bool
1218 New_since *string
1219 Baseline_file *string
1220 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001221 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001222 Aidl struct {
1223 Include_dirs []string
1224 Local_include_dirs []string
1225 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001226 Dist struct {
1227 Targets []string
1228 Dest *string
1229 Dir *string
1230 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001231 }{}
1232
Paul Duffinda364252020-04-28 14:08:32 +01001233 // The stubs source processing uses the same compile time classpath when extracting the
1234 // API from the implementation library as it does when compiling it. i.e. the same
1235 // * sdk version
1236 // * system_modules
1237 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001238
Paul Duffina377e4c2020-04-29 13:30:54 +01001239 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001240
1241 // If stubs_source_visibility is not set then the created module will use the
1242 // visibility of this module.
1243 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1244 props.Visibility = visibility
1245
Paul Duffinc5d954a2020-05-16 18:54:24 +01001246 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1247 props.Sdk_version = module.deviceProperties.Sdk_version
1248 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001249 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001250 // A droiddoc module has only one Libs property and doesn't distinguish between
1251 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001252 props.Libs = module.properties.Libs
1253 props.Libs = append(props.Libs, module.properties.Static_libs...)
1254 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1255 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1256 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001257
Paul Duffin2ce1e812020-05-20 19:35:27 +01001258 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001259 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1260 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1261
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001262 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001263 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001264 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001265 }
1266 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001267 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001268 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1269 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001270 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001271 disabledWarnings := []string{
1272 "MissingPermission",
1273 "BroadcastBehavior",
1274 "HiddenSuperclass",
1275 "DeprecationMismatch",
1276 "UnavailableSymbol",
1277 "SdkConstant",
1278 "HiddenTypeParameter",
1279 "Todo",
1280 "Typo",
1281 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001282 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001283
Paul Duffina377e4c2020-04-29 13:30:54 +01001284 if !createStubSources {
1285 // Stubs are not required.
1286 props.Generate_stubs = proptools.BoolPtr(false)
1287 }
1288
Paul Duffin3c7c3472020-04-07 18:50:10 +01001289 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001290 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001291 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001292 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001293
Paul Duffina377e4c2020-04-29 13:30:54 +01001294 if createApi {
1295 // List of APIs identified from the provided source files are created. They are later
1296 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1297 // last-released (a.k.a numbered) list of API.
1298 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1299 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1300 apiDir := module.getApiDir()
1301 currentApiFileName = path.Join(apiDir, currentApiFileName)
1302 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001303
Paul Duffina377e4c2020-04-29 13:30:54 +01001304 // check against the not-yet-release API
1305 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1306 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001307
Paul Duffina377e4c2020-04-29 13:30:54 +01001308 if !apiScope.unstable {
1309 // check against the latest released API
1310 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1311 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1312 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1313 module.latestRemovedApiFilegroupName(apiScope))
1314 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001315
Paul Duffina377e4c2020-04-29 13:30:54 +01001316 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1317 // Enable api lint.
1318 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1319 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001320
Paul Duffina377e4c2020-04-29 13:30:54 +01001321 // If it exists then pass a lint-baseline.txt through to droidstubs.
1322 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1323 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1324 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1325 if err != nil {
1326 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1327 }
1328 if len(paths) == 1 {
1329 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1330 } else if len(paths) != 0 {
1331 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1332 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001333 }
1334 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001335
Paul Duffina377e4c2020-04-29 13:30:54 +01001336 // Dist the api txt artifact for sdk builds.
1337 if !Bool(module.sdkLibraryProperties.No_dist) {
1338 props.Dist.Targets = []string{"sdk", "win_sdk"}
1339 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1340 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1341 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001342 }
1343
Colin Cross84dfc3d2019-09-25 11:33:01 -07001344 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001345}
1346
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001347func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1348 depTag := mctx.OtherModuleDependencyTag(dep)
1349 if depTag == xmlPermissionsFileTag {
1350 return true
1351 }
1352 return module.Library.DepIsInSameApex(mctx, dep)
1353}
1354
Jiyong Parkc678ad32018-04-10 13:07:10 +09001355// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001356func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001357 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001358 Name *string
1359 Lib_name *string
1360 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001361 }{
Paul Duffinf642a312020-06-12 17:46:39 +01001362 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001363 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1364 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001365 }
Jiyong Parke3833882020-02-17 17:28:10 +09001366
Jiyong Parke3833882020-02-17 17:28:10 +09001367 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001368}
1369
Paul Duffin50061512020-01-21 16:31:05 +00001370func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001371 var ver sdkVersion
1372 var kind sdkKind
1373 if s.usePrebuilt(ctx) {
1374 ver = s.version
1375 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001376 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001377 // We don't have prebuilt SDK for the specific sdkVersion.
1378 // Instead of breaking the build, fallback to use "system_current"
1379 ver = sdkVersionCurrent
1380 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001381 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001382
1383 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001384 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001385 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001386 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001387 if ctx.Config().AllowMissingDependencies() {
1388 return android.Paths{android.PathForSource(ctx, jar)}
1389 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001390 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001391 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001392 return nil
1393 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001394 return android.Paths{jarPath.Path()}
1395}
1396
Paul Duffinbf19a972020-05-26 13:21:35 +01001397// Get the apex name for module, "" if it is for platform.
1398func getApexNameForModule(module android.Module) string {
1399 if apex, ok := module.(android.ApexModule); ok {
1400 return apex.ApexName()
1401 }
1402
1403 return ""
1404}
1405
1406// Check to see if the other module is within the same named APEX as this module.
1407//
1408// If either this or the other module are on the platform then this will return
1409// false.
Paul Duffinf642a312020-06-12 17:46:39 +01001410func withinSameApexAs(module android.ApexModule, other android.Module) bool {
Paul Duffinbf19a972020-05-26 13:21:35 +01001411 name := module.ApexName()
1412 return name != "" && getApexNameForModule(other) == name
1413}
1414
Paul Duffin47624362020-05-20 12:19:10 +01001415func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park27fc4142020-05-28 00:19:53 +09001416 // If the client doesn't set sdk_version, but if this library prefers stubs over
1417 // the impl library, let's provide the widest API surface possible. To do so,
1418 // force override sdk_version to module_current so that the closest possible API
1419 // surface could be found in selectHeaderJarsForSdkVersion
1420 if module.defaultsToStubs() && !sdkVersion.specified() {
1421 sdkVersion = sdkSpecFrom("module_current")
1422 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001423
Paul Duffin2e7ed652020-05-26 18:13:57 +01001424 // Only provide access to the implementation library if it is actually built.
1425 if module.requiresRuntimeImplementationLibrary() {
1426 // Check any special cases for java_sdk_library.
1427 //
1428 // Only allow access to the implementation library in the following condition:
1429 // * No sdk_version specified on the referencing module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001430 // * The referencing module is in the same apex as this.
Paul Duffinf642a312020-06-12 17:46:39 +01001431 if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
Paul Duffin2e7ed652020-05-26 18:13:57 +01001432 if headerJars {
1433 return module.HeaderJars()
1434 } else {
1435 return module.ImplementationJars()
1436 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001437 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001438 }
Paul Duffin47624362020-05-20 12:19:10 +01001439
Paul Duffina3fb67d2020-05-20 14:20:02 +01001440 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001441}
1442
Sundong Ahn241cd372018-07-13 16:16:44 +09001443// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001444func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1445 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1446}
1447
1448// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001449func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001450 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001451}
1452
Sundong Ahn80a87b32019-05-13 15:02:50 +09001453func (module *SdkLibrary) SetNoDist() {
1454 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1455}
1456
Colin Cross571cccf2019-02-04 11:22:08 -08001457var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1458
Jiyong Park82484c02018-04-23 21:41:26 +09001459func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001460 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001461 return &[]string{}
1462 }).(*[]string)
1463}
1464
Paul Duffin749f98f2019-12-30 17:23:46 +00001465func (module *SdkLibrary) getApiDir() string {
1466 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1467}
1468
Jiyong Parkc678ad32018-04-10 13:07:10 +09001469// For a java_sdk_library module, create internal modules for stubs, docs,
1470// runtime libs and xml file. If requested, the stubs and docs are created twice
1471// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001472func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1473 // If the module has been disabled then don't create any child modules.
1474 if !module.Enabled() {
1475 return
1476 }
1477
Paul Duffinc5d954a2020-05-16 18:54:24 +01001478 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001479 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001480 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001481 }
1482
Paul Duffin37e0b772019-12-30 17:20:10 +00001483 // If this builds against standard libraries (i.e. is not part of the core libraries)
1484 // then assume it provides both system and test apis. Otherwise, assume it does not and
1485 // also assume it does not contribute to the dist build.
1486 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1487 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001488 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001489 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1490
Inseob Kim8098faa2019-03-18 10:19:51 +09001491 missing_current_api := false
1492
Paul Duffin3a254982020-04-28 10:44:03 +01001493 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001494
Paul Duffin749f98f2019-12-30 17:23:46 +00001495 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001496 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001497 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001498 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001499 p := android.ExistentPathForSource(mctx, path)
1500 if !p.Valid() {
1501 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1502 missing_current_api = true
1503 }
1504 }
1505 }
1506
1507 if missing_current_api {
1508 script := "build/soong/scripts/gen-java-current-api-files.sh"
1509 p := android.ExistentPathForSource(mctx, script)
1510
1511 if !p.Valid() {
1512 panic(fmt.Sprintf("script file %s doesn't exist", script))
1513 }
1514
1515 mctx.ModuleErrorf("One or more current api files are missing. "+
1516 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001517 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001518 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001519 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001520 return
1521 }
1522
Paul Duffin3a254982020-04-28 10:44:03 +01001523 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001524 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001525 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001526
1527 // If the args needed to generate the stubs and API are the same then they
1528 // can be generated in a single invocation of metalava, otherwise they will
1529 // need separate invocations.
1530 if scope.createStubsSourceAndApiTogether {
1531 // Use the stubs source name for legacy reasons.
1532 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1533 } else {
1534 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1535
1536 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001537 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001538 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1539 }
1540
Paul Duffind1b3a922020-01-22 11:57:20 +00001541 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001542 }
1543
Paul Duffind11e78e2020-05-15 20:37:11 +01001544 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001545 // Create child module to create an implementation library.
1546 //
1547 // This temporarily creates a second implementation library that can be explicitly
1548 // referenced.
1549 //
1550 // TODO(b/156618935) - update comment once only one implementation library is created.
1551 module.createImplLibrary(mctx)
1552
Paul Duffind11e78e2020-05-15 20:37:11 +01001553 // Only create an XML permissions file that declares the library as being usable
1554 // as a shared library if required.
1555 if module.sharedLibrary() {
1556 module.createXmlFile(mctx)
1557 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001558
1559 // record java_sdk_library modules so that they are exported to make
1560 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1561 javaSdkLibrariesLock.Lock()
1562 defer javaSdkLibrariesLock.Unlock()
1563 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1564 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001565}
1566
1567func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Cross1c14b4e2020-06-15 16:09:53 -07001568 module.addHostAndDeviceProperties()
1569 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001570
Paul Duffin64e61992020-05-15 10:20:31 +01001571 module.initSdkLibraryComponent(&module.ModuleBase)
1572
Paul Duffinc5d954a2020-05-16 18:54:24 +01001573 module.properties.Installable = proptools.BoolPtr(true)
1574 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001575}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001576
Paul Duffind11e78e2020-05-15 20:37:11 +01001577func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1578 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1579}
1580
Jiyong Park27fc4142020-05-28 00:19:53 +09001581func (module *SdkLibrary) defaultsToStubs() bool {
1582 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1583}
1584
Paul Duffin1a724e62020-05-08 13:44:43 +01001585// Defines how to name the individual component modules the sdk library creates.
1586type sdkLibraryComponentNamingScheme interface {
1587 stubsLibraryModuleName(scope *apiScope, baseName string) string
1588
1589 stubsSourceModuleName(scope *apiScope, baseName string) string
1590
1591 apiModuleName(scope *apiScope, baseName string) string
1592}
1593
1594type defaultNamingScheme struct {
1595}
1596
1597func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1598 return scope.stubsLibraryModuleName(baseName)
1599}
1600
1601func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1602 return scope.stubsSourceModuleName(baseName)
1603}
1604
1605func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1606 return scope.apiModuleName(baseName)
1607}
1608
1609var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1610
Paul Duffindef8a892020-05-08 15:36:30 +01001611type frameworkModulesNamingScheme struct {
1612}
1613
1614func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1615 suffix := scope.name
1616 if scope == apiScopeModuleLib {
1617 suffix = "module_libs_"
1618 }
1619 return suffix
1620}
1621
1622func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1623 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1624}
1625
1626func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1627 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1628}
1629
1630func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1631 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1632}
1633
1634var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1635
Anton Hansson0bd88d02020-05-25 12:20:51 +01001636func moduleStubLinkType(name string) (stub bool, ret linkType) {
1637 // This suffix-based approach is fragile and could potentially mis-trigger.
1638 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1639 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1640 return true, javaSdk
1641 }
1642 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1643 return true, javaSystem
1644 }
1645 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1646 return true, javaModule
1647 }
1648 if strings.HasSuffix(name, ".stubs.test") {
1649 return true, javaSystem
1650 }
1651 return false, javaPlatform
1652}
1653
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001654// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1655// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1656// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1657// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1658// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001659func SdkLibraryFactory() android.Module {
1660 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001661
1662 // Initialize information common between source and prebuilt.
1663 module.initCommon(&module.ModuleBase)
1664
Inseob Kimc0907f12019-02-08 21:00:45 +09001665 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001666 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001667 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001668
1669 // Initialize the map from scope to scope specific properties.
1670 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1671 for _, scope := range allApiScopes {
1672 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1673 }
1674 module.scopeToProperties = scopeToProperties
1675
Paul Duffin344c4ee2020-04-29 23:35:13 +01001676 // Add the properties containing visibility rules so that they are checked.
Paul Duffin9d582cc2020-05-16 15:52:12 +01001677 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001678 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1679 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1680
Paul Duffin1a724e62020-05-08 13:44:43 +01001681 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001682 // If no implementation is required then it cannot be used as a shared library
1683 // either.
1684 if !module.requiresRuntimeImplementationLibrary() {
1685 // If shared_library has been explicitly set to true then it is incompatible
1686 // with api_only: true.
1687 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1688 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1689 }
1690 // Set shared_library: false.
1691 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1692 }
1693
Paul Duffin1a724e62020-05-08 13:44:43 +01001694 if module.initCommonAfterDefaultsApplied(ctx) {
1695 module.CreateInternalModules(ctx)
1696 }
1697 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001698 return module
1699}
Colin Cross79c7c262019-04-17 11:11:46 -07001700
1701//
1702// SDK library prebuilts
1703//
1704
Paul Duffin56d44902020-01-31 13:36:25 +00001705// Properties associated with each api scope.
1706type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001707 Jars []string `android:"path"`
1708
1709 Sdk_version *string
1710
Colin Cross79c7c262019-04-17 11:11:46 -07001711 // List of shared java libs that this module has dependencies to
1712 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001713
Paul Duffin5fb82132020-04-29 20:45:27 +01001714 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001715 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001716
1717 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001718 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001719
1720 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001721 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001722}
1723
Paul Duffin56d44902020-01-31 13:36:25 +00001724type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001725 // List of shared java libs, common to all scopes, that this module has
1726 // dependencies to
1727 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001728}
1729
Paul Duffinf642a312020-06-12 17:46:39 +01001730type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001731 android.ModuleBase
1732 android.DefaultableModuleBase
1733 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001734 android.ApexModuleBase
1735 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001736
1737 properties sdkLibraryImportProperties
1738
Paul Duffin6a2bd112020-04-07 19:27:04 +01001739 // Map from api scope to the scope specific property structure.
1740 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1741
Paul Duffin56d44902020-01-31 13:36:25 +00001742 commonToSdkLibraryAndImport
Paul Duffinf642a312020-06-12 17:46:39 +01001743
1744 // The reference to the implementation library created by the source module.
1745 // Is nil if the source module does not exist.
1746 implLibraryModule *Library
1747
1748 // The reference to the xml permissions module created by the source module.
1749 // Is nil if the source module does not exist.
1750 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001751}
1752
Paul Duffinf642a312020-06-12 17:46:39 +01001753var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001754
Paul Duffin6a2bd112020-04-07 19:27:04 +01001755// The type of a structure that contains a field of type sdkLibraryScopeProperties
1756// for each apiscope in allApiScopes, e.g. something like:
1757// struct {
1758// Public sdkLibraryScopeProperties
1759// System sdkLibraryScopeProperties
1760// ...
1761// }
1762var allScopeStructType = createAllScopePropertiesStructType()
1763
1764// Dynamically create a structure type for each apiscope in allApiScopes.
1765func createAllScopePropertiesStructType() reflect.Type {
1766 var fields []reflect.StructField
1767 for _, apiScope := range allApiScopes {
1768 field := reflect.StructField{
1769 Name: apiScope.fieldName,
1770 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1771 }
1772 fields = append(fields, field)
1773 }
1774
1775 return reflect.StructOf(fields)
1776}
1777
1778// Create an instance of the scope specific structure type and return a map
1779// from apiscope to a pointer to each scope specific field.
1780func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1781 allScopePropertiesPtr := reflect.New(allScopeStructType)
1782 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1783 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1784
1785 for _, apiScope := range allApiScopes {
1786 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1787 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1788 }
1789
1790 return allScopePropertiesPtr.Interface(), scopeProperties
1791}
1792
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001793// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001794func sdkLibraryImportFactory() android.Module {
Paul Duffinf642a312020-06-12 17:46:39 +01001795 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001796
Paul Duffin6a2bd112020-04-07 19:27:04 +01001797 allScopeProperties, scopeToProperties := createPropertiesInstance()
1798 module.scopeProperties = scopeToProperties
1799 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001800
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001801 // Initialize information common between source and prebuilt.
1802 module.initCommon(&module.ModuleBase)
1803
Paul Duffin0bdcb272020-02-06 15:24:57 +00001804 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001805 android.InitApexModule(module)
1806 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001807 InitJavaModule(module, android.HostAndDeviceSupported)
1808
Paul Duffin1a724e62020-05-08 13:44:43 +01001809 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1810 if module.initCommonAfterDefaultsApplied(mctx) {
1811 module.createInternalModules(mctx)
1812 }
1813 })
Colin Cross79c7c262019-04-17 11:11:46 -07001814 return module
1815}
1816
Paul Duffinf642a312020-06-12 17:46:39 +01001817func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001818 return &module.prebuilt
1819}
1820
Paul Duffinf642a312020-06-12 17:46:39 +01001821func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001822 return module.prebuilt.Name(module.ModuleBase.Name())
1823}
1824
Paul Duffinf642a312020-06-12 17:46:39 +01001825func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001826
Paul Duffin50061512020-01-21 16:31:05 +00001827 // If the build is configured to use prebuilts then force this to be preferred.
1828 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1829 module.prebuilt.ForcePrefer()
1830 }
1831
Paul Duffin6a2bd112020-04-07 19:27:04 +01001832 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001833 if len(scopeProperties.Jars) == 0 {
1834 continue
1835 }
1836
Paul Duffinf6155722020-04-09 00:07:11 +01001837 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001838
Paul Duffin533f9c72020-05-20 16:18:00 +01001839 if len(scopeProperties.Stub_srcs) > 0 {
1840 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1841 }
Paul Duffin56d44902020-01-31 13:36:25 +00001842 }
Colin Cross79c7c262019-04-17 11:11:46 -07001843
1844 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1845 javaSdkLibrariesLock.Lock()
1846 defer javaSdkLibrariesLock.Unlock()
1847 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1848}
1849
Paul Duffinf642a312020-06-12 17:46:39 +01001850func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001851 // Creates a java import for the jar with ".stubs" suffix
1852 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001853 Name *string
1854 Sdk_version *string
1855 Libs []string
1856 Jars []string
1857 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001858 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001859 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001860 props.Sdk_version = scopeProperties.Sdk_version
1861 // Prepend any of the libs from the legacy public properties to the libs for each of the
1862 // scopes to avoid having to duplicate them in each scope.
1863 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1864 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001865
Paul Duffindd89a282020-05-13 16:08:09 +01001866 // The imports are preferred if the java_sdk_library_import is preferred.
1867 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001868
1869 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001870}
1871
Paul Duffinf642a312020-06-12 17:46:39 +01001872func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001873 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001874 Name *string
1875 Srcs []string
1876 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001877 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001878 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001879 props.Srcs = scopeProperties.Stub_srcs
1880 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001881
1882 // The stubs source is preferred if the java_sdk_library_import is preferred.
1883 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001884}
1885
Paul Duffinf642a312020-06-12 17:46:39 +01001886func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001887 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001888 if len(scopeProperties.Jars) == 0 {
1889 continue
1890 }
1891
1892 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001893 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001894
1895 if len(scopeProperties.Stub_srcs) > 0 {
1896 // Add dependencies to the prebuilt stubs source library
1897 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1898 }
Paul Duffin56d44902020-01-31 13:36:25 +00001899 }
Paul Duffinf642a312020-06-12 17:46:39 +01001900
1901 implName := module.implLibraryModuleName()
1902 if ctx.OtherModuleExists(implName) {
1903 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1904
1905 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1906 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1907 // Add dependency to the rule for generating the xml permissions file
1908 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1909 }
1910 }
Colin Cross79c7c262019-04-17 11:11:46 -07001911}
1912
Paul Duffinf642a312020-06-12 17:46:39 +01001913func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1914 depTag := mctx.OtherModuleDependencyTag(dep)
1915 if depTag == xmlPermissionsFileTag {
1916 return true
1917 }
1918
1919 // None of the other dependencies of the java_sdk_library_import are in the same apex
1920 // as the one that references this module.
1921 return false
1922}
1923
1924func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46fdda82020-05-14 15:39:10 +01001925 return module.commonOutputFiles(tag)
1926}
1927
Paul Duffinf642a312020-06-12 17:46:39 +01001928func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001929 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001930 ctx.VisitDirectDeps(func(to android.Module) {
1931 tag := ctx.OtherModuleDependencyTag(to)
1932
Paul Duffin533f9c72020-05-20 16:18:00 +01001933 // Extract information from any of the scope specific dependencies.
1934 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1935 apiScope := scopeTag.apiScope
1936 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1937
1938 // Extract information from the dependency. The exact information extracted
1939 // is determined by the nature of the dependency which is determined by the tag.
1940 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinf642a312020-06-12 17:46:39 +01001941 } else if tag == implLibraryTag {
1942 if implLibrary, ok := to.(*Library); ok {
1943 module.implLibraryModule = implLibrary
1944 } else {
1945 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1946 }
1947 } else if tag == xmlPermissionsFileTag {
1948 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1949 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1950 } else {
1951 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1952 }
Colin Cross79c7c262019-04-17 11:11:46 -07001953 }
1954 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001955
1956 // Populate the scope paths with information from the properties.
1957 for apiScope, scopeProperties := range module.scopeProperties {
1958 if len(scopeProperties.Jars) == 0 {
1959 continue
1960 }
1961
1962 paths := module.getScopePathsCreateIfNeeded(apiScope)
1963 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1964 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1965 }
Colin Cross79c7c262019-04-17 11:11:46 -07001966}
1967
Paul Duffinf642a312020-06-12 17:46:39 +01001968func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1969
1970 // For consistency with SdkLibrary make the implementation jar available to libraries that
1971 // are within the same APEX.
1972 implLibraryModule := module.implLibraryModule
1973 if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
1974 if headerJars {
1975 return implLibraryModule.HeaderJars()
1976 } else {
1977 return implLibraryModule.ImplementationJars()
1978 }
1979 }
1980
Paul Duffina3fb67d2020-05-20 14:20:02 +01001981 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001982}
1983
Colin Cross79c7c262019-04-17 11:11:46 -07001984// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01001985func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001986 // This module is just a wrapper for the prebuilt stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01001987 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07001988}
1989
1990// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01001991func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001992 // This module is just a wrapper for the stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01001993 return module.sdkJars(ctx, sdkVersion, false)
1994}
1995
1996// to satisfy apex.javaDependency interface
1997func (module *SdkLibraryImport) DexJar() android.Path {
1998 if module.implLibraryModule == nil {
1999 return nil
2000 } else {
2001 return module.implLibraryModule.DexJar()
2002 }
2003}
2004
2005// to satisfy apex.javaDependency interface
2006func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2007 if module.implLibraryModule == nil {
2008 return nil
2009 } else {
2010 return module.implLibraryModule.JacocoReportClassesFile()
2011 }
2012}
2013
2014// to satisfy apex.javaDependency interface
Colin Cross5bc17442020-07-21 20:31:17 -07002015func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2016 if module.implLibraryModule == nil {
2017 return LintDepSets{}
2018 } else {
2019 return module.implLibraryModule.LintDepSets()
2020 }
2021}
2022
2023// to satisfy apex.javaDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01002024func (module *SdkLibraryImport) Stem() string {
2025 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002026}
Jiyong Parke3833882020-02-17 17:28:10 +09002027
Paul Duffin9ee66da2020-06-17 16:59:43 +01002028var _ ApexDependency = (*SdkLibraryImport)(nil)
2029
2030// to satisfy java.ApexDependency interface
2031func (module *SdkLibraryImport) HeaderJars() android.Paths {
2032 if module.implLibraryModule == nil {
2033 return nil
2034 } else {
2035 return module.implLibraryModule.HeaderJars()
2036 }
2037}
2038
2039// to satisfy java.ApexDependency interface
2040func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2041 if module.implLibraryModule == nil {
2042 return nil
2043 } else {
2044 return module.implLibraryModule.ImplementationAndResourcesJars()
2045 }
2046}
2047
Jiyong Parke3833882020-02-17 17:28:10 +09002048//
2049// java_sdk_library_xml
2050//
2051type sdkLibraryXml struct {
2052 android.ModuleBase
2053 android.DefaultableModuleBase
2054 android.ApexModuleBase
2055
2056 properties sdkLibraryXmlProperties
2057
2058 outputFilePath android.OutputPath
2059 installDirPath android.InstallPath
2060}
2061
2062type sdkLibraryXmlProperties struct {
2063 // canonical name of the lib
2064 Lib_name *string
2065}
2066
2067// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2068// Not to be used directly by users. java_sdk_library internally uses this.
2069func sdkLibraryXmlFactory() android.Module {
2070 module := &sdkLibraryXml{}
2071
2072 module.AddProperties(&module.properties)
2073
2074 android.InitApexModule(module)
2075 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2076
2077 return module
2078}
2079
2080// from android.PrebuiltEtcModule
2081func (module *sdkLibraryXml) SubDir() string {
2082 return "permissions"
2083}
2084
2085// from android.PrebuiltEtcModule
2086func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2087 return module.outputFilePath
2088}
2089
2090// from android.ApexModule
2091func (module *sdkLibraryXml) AvailableFor(what string) bool {
2092 return true
2093}
2094
2095func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2096 // do nothing
2097}
2098
2099// File path to the runtime implementation library
2100func (module *sdkLibraryXml) implPath() string {
2101 implName := proptools.String(module.properties.Lib_name)
2102 if apexName := module.ApexName(); apexName != "" {
2103 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
2104 // In most cases, this works fine. But when apex_name is set or override_apex is used
2105 // this can be wrong.
2106 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2107 }
2108 partition := "system"
2109 if module.SocSpecific() {
2110 partition = "vendor"
2111 } else if module.DeviceSpecific() {
2112 partition = "odm"
2113 } else if module.ProductSpecific() {
2114 partition = "product"
2115 } else if module.SystemExtSpecific() {
2116 partition = "system_ext"
2117 }
2118 return "/" + partition + "/framework/" + implName + ".jar"
2119}
2120
2121func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2122 libName := proptools.String(module.properties.Lib_name)
2123 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2124
2125 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2126 rule := android.NewRuleBuilder()
2127 rule.Command().
2128 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2129 Output(module.outputFilePath)
2130
2131 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2132
2133 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2134}
2135
2136func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2137 if !module.IsForPlatform() {
2138 return []android.AndroidMkEntries{android.AndroidMkEntries{
2139 Disabled: true,
2140 }}
2141 }
2142
2143 return []android.AndroidMkEntries{android.AndroidMkEntries{
2144 Class: "ETC",
2145 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2146 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2147 func(entries *android.AndroidMkEntries) {
2148 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2149 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2150 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2151 },
2152 },
2153 }}
2154}
Paul Duffin61871622020-02-10 13:37:10 +00002155
2156type sdkLibrarySdkMemberType struct {
2157 android.SdkMemberTypeBase
2158}
2159
2160func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2161 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2162}
2163
2164func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2165 _, ok := module.(*SdkLibrary)
2166 return ok
2167}
2168
2169func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2170 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2171}
2172
2173func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2174 return &sdkLibrarySdkMemberProperties{}
2175}
2176
2177type sdkLibrarySdkMemberProperties struct {
2178 android.SdkMemberPropertiesBase
2179
2180 // Scope to per scope properties.
2181 Scopes map[*apiScope]scopeProperties
2182
2183 // Additional libraries that the exported stubs libraries depend upon.
2184 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01002185
2186 // The Java stubs source files.
2187 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01002188
2189 // The naming scheme.
2190 Naming_scheme *string
Paul Duffina84756c2020-05-26 20:57:10 +01002191
2192 // True if the java_sdk_library_import is for a shared library, false
2193 // otherwise.
2194 Shared_library *bool
Paul Duffin61871622020-02-10 13:37:10 +00002195}
2196
2197type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01002198 Jars android.Paths
2199 StubsSrcJar android.Path
2200 CurrentApiFile android.Path
2201 RemovedApiFile android.Path
2202 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00002203}
2204
2205func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2206 sdk := variant.(*SdkLibrary)
2207
2208 s.Scopes = make(map[*apiScope]scopeProperties)
2209 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01002210 paths := sdk.findScopePaths(apiScope)
2211 if paths == nil {
2212 continue
2213 }
2214
Paul Duffin61871622020-02-10 13:37:10 +00002215 jars := paths.stubsImplPath
2216 if len(jars) > 0 {
2217 properties := scopeProperties{}
2218 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01002219 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01002220 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin86672f62020-06-19 18:39:55 +01002221 if paths.currentApiFilePath.Valid() {
2222 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2223 }
2224 if paths.removedApiFilePath.Valid() {
2225 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2226 }
Paul Duffin61871622020-02-10 13:37:10 +00002227 s.Scopes[apiScope] = properties
2228 }
2229 }
2230
2231 s.Libs = sdk.properties.Libs
Paul Duffind11e78e2020-05-15 20:37:11 +01002232 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffina84756c2020-05-26 20:57:10 +01002233 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin61871622020-02-10 13:37:10 +00002234}
2235
2236func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01002237 if s.Naming_scheme != nil {
2238 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2239 }
Paul Duffina84756c2020-05-26 20:57:10 +01002240 if s.Shared_library != nil {
2241 propertySet.AddProperty("shared_library", *s.Shared_library)
2242 }
Paul Duffinf8e08b22020-05-13 16:54:55 +01002243
Paul Duffin61871622020-02-10 13:37:10 +00002244 for _, apiScope := range allApiScopes {
2245 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01002246 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00002247
Paul Duffinf488ef22020-04-09 00:10:17 +01002248 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2249
Paul Duffin61871622020-02-10 13:37:10 +00002250 var jars []string
2251 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01002252 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00002253 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2254 jars = append(jars, dest)
2255 }
2256 scopeSet.AddProperty("jars", jars)
2257
Paul Duffinf488ef22020-04-09 00:10:17 +01002258 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2259 // the source files are also unpacked.
2260 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2261 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2262 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2263
Paul Duffin75dcc802020-04-09 01:08:11 +01002264 if properties.CurrentApiFile != nil {
2265 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2266 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2267 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2268 }
2269
2270 if properties.RemovedApiFile != nil {
2271 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffinb1787352020-06-02 13:00:02 +01002272 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin75dcc802020-04-09 01:08:11 +01002273 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2274 }
2275
Paul Duffin61871622020-02-10 13:37:10 +00002276 if properties.SdkVersion != "" {
2277 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2278 }
2279 }
2280 }
2281
2282 if len(s.Libs) > 0 {
2283 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2284 }
2285}