blob: a6549ea5facd48c96c167d50b25e09c626151585 [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) {
1091 props := struct {
1092 Name *string
1093 Visibility []string
1094 }{
1095 Name: proptools.StringPtr(module.implLibraryModuleName()),
1096 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
1097 }
1098
1099 properties := []interface{}{
1100 &module.properties,
1101 &module.protoProperties,
1102 &module.deviceProperties,
1103 &module.dexpreoptProperties,
1104 &props,
1105 module.sdkComponentPropertiesForChildLibrary(),
1106 }
1107 mctx.CreateModule(LibraryFactory, properties...)
1108}
1109
Jiyong Parkc678ad32018-04-10 13:07:10 +09001110// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001111func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001112 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001113 Name *string
1114 Visibility []string
1115 Srcs []string
1116 Installable *bool
1117 Sdk_version *string
1118 System_modules *string
1119 Patch_module *string
1120 Libs []string
1121 Compile_dex *bool
1122 Java_version *string
1123 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001124 Pdk struct {
1125 Enabled *bool
1126 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001127 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001128 Openjdk9 struct {
1129 Srcs []string
1130 Javacflags []string
1131 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001132 Dist struct {
1133 Targets []string
1134 Dest *string
1135 Dir *string
1136 Tag *string
1137 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001138 }{}
1139
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001140 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +01001141
1142 // If stubs_library_visibility is not set then the created module will use the
1143 // visibility of this module.
1144 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1145 props.Visibility = visibility
1146
Jiyong Parkc678ad32018-04-10 13:07:10 +09001147 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001148 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001149 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001150 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001151 props.System_modules = module.deviceProperties.System_modules
1152 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001153 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001154 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +01001155 // The stub-annotations library contains special versions of the annotations
1156 // with CLASS retention policy, so that they're kept.
1157 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1158 props.Libs = append(props.Libs, "stub-annotations")
1159 }
Jiyong Park82484c02018-04-23 21:41:26 +09001160 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001161 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1162 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hanssoncf4dd4c2020-05-21 09:21:57 +01001163 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1164 // interop with older developer tools that don't support 1.9.
1165 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinc5d954a2020-05-16 18:54:24 +01001166 if module.deviceProperties.Compile_dex != nil {
1167 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001168 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001169
Anton Hansson6bb88102020-03-27 19:43:19 +00001170 // Dist the class jar artifact for sdk builds.
1171 if !Bool(module.sdkLibraryProperties.No_dist) {
1172 props.Dist.Targets = []string{"sdk", "win_sdk"}
1173 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1174 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1175 props.Dist.Tag = proptools.StringPtr(".jar")
1176 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001177
Paul Duffin64e61992020-05-15 10:20:31 +01001178 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001179}
1180
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001181// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001182// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +01001183func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001184 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001185 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001186 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001187 Srcs []string
1188 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001189 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001190 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001191 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001192 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001193 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001194 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +01001195 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001196 Merge_annotations_dirs []string
1197 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001198 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001199 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001200 Current ApiToCheck
1201 Last_released ApiToCheck
1202 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001203
1204 Api_lint struct {
1205 Enabled *bool
1206 New_since *string
1207 Baseline_file *string
1208 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001209 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001210 Aidl struct {
1211 Include_dirs []string
1212 Local_include_dirs []string
1213 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001214 Dist struct {
1215 Targets []string
1216 Dest *string
1217 Dir *string
1218 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001219 }{}
1220
Paul Duffinda364252020-04-28 14:08:32 +01001221 // The stubs source processing uses the same compile time classpath when extracting the
1222 // API from the implementation library as it does when compiling it. i.e. the same
1223 // * sdk version
1224 // * system_modules
1225 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001226
Paul Duffina377e4c2020-04-29 13:30:54 +01001227 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001228
1229 // If stubs_source_visibility is not set then the created module will use the
1230 // visibility of this module.
1231 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1232 props.Visibility = visibility
1233
Paul Duffinc5d954a2020-05-16 18:54:24 +01001234 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1235 props.Sdk_version = module.deviceProperties.Sdk_version
1236 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001237 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001238 // A droiddoc module has only one Libs property and doesn't distinguish between
1239 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001240 props.Libs = module.properties.Libs
1241 props.Libs = append(props.Libs, module.properties.Static_libs...)
1242 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1243 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1244 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001245
Paul Duffin2ce1e812020-05-20 19:35:27 +01001246 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001247 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1248 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1249
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001250 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001251 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001252 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001253 }
1254 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001255 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001256 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1257 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001258 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001259 disabledWarnings := []string{
1260 "MissingPermission",
1261 "BroadcastBehavior",
1262 "HiddenSuperclass",
1263 "DeprecationMismatch",
1264 "UnavailableSymbol",
1265 "SdkConstant",
1266 "HiddenTypeParameter",
1267 "Todo",
1268 "Typo",
1269 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001270 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001271
Paul Duffina377e4c2020-04-29 13:30:54 +01001272 if !createStubSources {
1273 // Stubs are not required.
1274 props.Generate_stubs = proptools.BoolPtr(false)
1275 }
1276
Paul Duffin3c7c3472020-04-07 18:50:10 +01001277 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001278 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001279 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001280 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001281
Paul Duffina377e4c2020-04-29 13:30:54 +01001282 if createApi {
1283 // List of APIs identified from the provided source files are created. They are later
1284 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1285 // last-released (a.k.a numbered) list of API.
1286 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1287 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1288 apiDir := module.getApiDir()
1289 currentApiFileName = path.Join(apiDir, currentApiFileName)
1290 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001291
Paul Duffina377e4c2020-04-29 13:30:54 +01001292 // check against the not-yet-release API
1293 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1294 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001295
Paul Duffina377e4c2020-04-29 13:30:54 +01001296 if !apiScope.unstable {
1297 // check against the latest released API
1298 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1299 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1300 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1301 module.latestRemovedApiFilegroupName(apiScope))
1302 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001303
Paul Duffina377e4c2020-04-29 13:30:54 +01001304 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1305 // Enable api lint.
1306 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1307 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001308
Paul Duffina377e4c2020-04-29 13:30:54 +01001309 // If it exists then pass a lint-baseline.txt through to droidstubs.
1310 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1311 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1312 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1313 if err != nil {
1314 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1315 }
1316 if len(paths) == 1 {
1317 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1318 } else if len(paths) != 0 {
1319 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1320 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001321 }
1322 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001323
Paul Duffina377e4c2020-04-29 13:30:54 +01001324 // Dist the api txt artifact for sdk builds.
1325 if !Bool(module.sdkLibraryProperties.No_dist) {
1326 props.Dist.Targets = []string{"sdk", "win_sdk"}
1327 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1328 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1329 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001330 }
1331
Colin Cross84dfc3d2019-09-25 11:33:01 -07001332 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001333}
1334
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001335func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1336 depTag := mctx.OtherModuleDependencyTag(dep)
1337 if depTag == xmlPermissionsFileTag {
1338 return true
1339 }
1340 return module.Library.DepIsInSameApex(mctx, dep)
1341}
1342
Jiyong Parkc678ad32018-04-10 13:07:10 +09001343// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001344func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001345 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001346 Name *string
1347 Lib_name *string
1348 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001349 }{
Paul Duffinf642a312020-06-12 17:46:39 +01001350 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001351 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1352 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001353 }
Jiyong Parke3833882020-02-17 17:28:10 +09001354
Jiyong Parke3833882020-02-17 17:28:10 +09001355 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001356}
1357
Paul Duffin50061512020-01-21 16:31:05 +00001358func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001359 var ver sdkVersion
1360 var kind sdkKind
1361 if s.usePrebuilt(ctx) {
1362 ver = s.version
1363 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001364 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001365 // We don't have prebuilt SDK for the specific sdkVersion.
1366 // Instead of breaking the build, fallback to use "system_current"
1367 ver = sdkVersionCurrent
1368 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001369 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001370
1371 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001372 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001373 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001374 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001375 if ctx.Config().AllowMissingDependencies() {
1376 return android.Paths{android.PathForSource(ctx, jar)}
1377 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001378 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001379 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001380 return nil
1381 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001382 return android.Paths{jarPath.Path()}
1383}
1384
Paul Duffinbf19a972020-05-26 13:21:35 +01001385// Get the apex name for module, "" if it is for platform.
1386func getApexNameForModule(module android.Module) string {
1387 if apex, ok := module.(android.ApexModule); ok {
1388 return apex.ApexName()
1389 }
1390
1391 return ""
1392}
1393
1394// Check to see if the other module is within the same named APEX as this module.
1395//
1396// If either this or the other module are on the platform then this will return
1397// false.
Paul Duffinf642a312020-06-12 17:46:39 +01001398func withinSameApexAs(module android.ApexModule, other android.Module) bool {
Paul Duffinbf19a972020-05-26 13:21:35 +01001399 name := module.ApexName()
1400 return name != "" && getApexNameForModule(other) == name
1401}
1402
Paul Duffin47624362020-05-20 12:19:10 +01001403func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park27fc4142020-05-28 00:19:53 +09001404 // If the client doesn't set sdk_version, but if this library prefers stubs over
1405 // the impl library, let's provide the widest API surface possible. To do so,
1406 // force override sdk_version to module_current so that the closest possible API
1407 // surface could be found in selectHeaderJarsForSdkVersion
1408 if module.defaultsToStubs() && !sdkVersion.specified() {
1409 sdkVersion = sdkSpecFrom("module_current")
1410 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001411
Paul Duffin2e7ed652020-05-26 18:13:57 +01001412 // Only provide access to the implementation library if it is actually built.
1413 if module.requiresRuntimeImplementationLibrary() {
1414 // Check any special cases for java_sdk_library.
1415 //
1416 // Only allow access to the implementation library in the following condition:
1417 // * No sdk_version specified on the referencing module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001418 // * The referencing module is in the same apex as this.
Paul Duffinf642a312020-06-12 17:46:39 +01001419 if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
Paul Duffin2e7ed652020-05-26 18:13:57 +01001420 if headerJars {
1421 return module.HeaderJars()
1422 } else {
1423 return module.ImplementationJars()
1424 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001425 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001426 }
Paul Duffin47624362020-05-20 12:19:10 +01001427
Paul Duffina3fb67d2020-05-20 14:20:02 +01001428 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001429}
1430
Sundong Ahn241cd372018-07-13 16:16:44 +09001431// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001432func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1433 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1434}
1435
1436// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001437func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001438 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001439}
1440
Sundong Ahn80a87b32019-05-13 15:02:50 +09001441func (module *SdkLibrary) SetNoDist() {
1442 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1443}
1444
Colin Cross571cccf2019-02-04 11:22:08 -08001445var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1446
Jiyong Park82484c02018-04-23 21:41:26 +09001447func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001448 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001449 return &[]string{}
1450 }).(*[]string)
1451}
1452
Paul Duffin749f98f2019-12-30 17:23:46 +00001453func (module *SdkLibrary) getApiDir() string {
1454 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1455}
1456
Jiyong Parkc678ad32018-04-10 13:07:10 +09001457// For a java_sdk_library module, create internal modules for stubs, docs,
1458// runtime libs and xml file. If requested, the stubs and docs are created twice
1459// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001460func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1461 // If the module has been disabled then don't create any child modules.
1462 if !module.Enabled() {
1463 return
1464 }
1465
Paul Duffinc5d954a2020-05-16 18:54:24 +01001466 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001467 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001468 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001469 }
1470
Paul Duffin37e0b772019-12-30 17:20:10 +00001471 // If this builds against standard libraries (i.e. is not part of the core libraries)
1472 // then assume it provides both system and test apis. Otherwise, assume it does not and
1473 // also assume it does not contribute to the dist build.
1474 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1475 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001476 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001477 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1478
Inseob Kim8098faa2019-03-18 10:19:51 +09001479 missing_current_api := false
1480
Paul Duffin3a254982020-04-28 10:44:03 +01001481 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001482
Paul Duffin749f98f2019-12-30 17:23:46 +00001483 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001484 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001485 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001486 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001487 p := android.ExistentPathForSource(mctx, path)
1488 if !p.Valid() {
1489 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1490 missing_current_api = true
1491 }
1492 }
1493 }
1494
1495 if missing_current_api {
1496 script := "build/soong/scripts/gen-java-current-api-files.sh"
1497 p := android.ExistentPathForSource(mctx, script)
1498
1499 if !p.Valid() {
1500 panic(fmt.Sprintf("script file %s doesn't exist", script))
1501 }
1502
1503 mctx.ModuleErrorf("One or more current api files are missing. "+
1504 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001505 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001506 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001507 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001508 return
1509 }
1510
Paul Duffin3a254982020-04-28 10:44:03 +01001511 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001512 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001513 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001514
1515 // If the args needed to generate the stubs and API are the same then they
1516 // can be generated in a single invocation of metalava, otherwise they will
1517 // need separate invocations.
1518 if scope.createStubsSourceAndApiTogether {
1519 // Use the stubs source name for legacy reasons.
1520 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1521 } else {
1522 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1523
1524 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001525 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001526 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1527 }
1528
Paul Duffind1b3a922020-01-22 11:57:20 +00001529 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001530 }
1531
Paul Duffind11e78e2020-05-15 20:37:11 +01001532 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001533 // Create child module to create an implementation library.
1534 //
1535 // This temporarily creates a second implementation library that can be explicitly
1536 // referenced.
1537 //
1538 // TODO(b/156618935) - update comment once only one implementation library is created.
1539 module.createImplLibrary(mctx)
1540
Paul Duffind11e78e2020-05-15 20:37:11 +01001541 // Only create an XML permissions file that declares the library as being usable
1542 // as a shared library if required.
1543 if module.sharedLibrary() {
1544 module.createXmlFile(mctx)
1545 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001546
1547 // record java_sdk_library modules so that they are exported to make
1548 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1549 javaSdkLibrariesLock.Lock()
1550 defer javaSdkLibrariesLock.Unlock()
1551 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1552 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001553}
1554
1555func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Cross1c14b4e2020-06-15 16:09:53 -07001556 module.addHostAndDeviceProperties()
1557 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001558
Paul Duffin64e61992020-05-15 10:20:31 +01001559 module.initSdkLibraryComponent(&module.ModuleBase)
1560
Paul Duffinc5d954a2020-05-16 18:54:24 +01001561 module.properties.Installable = proptools.BoolPtr(true)
1562 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001563}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001564
Paul Duffind11e78e2020-05-15 20:37:11 +01001565func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1566 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1567}
1568
Jiyong Park27fc4142020-05-28 00:19:53 +09001569func (module *SdkLibrary) defaultsToStubs() bool {
1570 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1571}
1572
Paul Duffin1a724e62020-05-08 13:44:43 +01001573// Defines how to name the individual component modules the sdk library creates.
1574type sdkLibraryComponentNamingScheme interface {
1575 stubsLibraryModuleName(scope *apiScope, baseName string) string
1576
1577 stubsSourceModuleName(scope *apiScope, baseName string) string
1578
1579 apiModuleName(scope *apiScope, baseName string) string
1580}
1581
1582type defaultNamingScheme struct {
1583}
1584
1585func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1586 return scope.stubsLibraryModuleName(baseName)
1587}
1588
1589func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1590 return scope.stubsSourceModuleName(baseName)
1591}
1592
1593func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1594 return scope.apiModuleName(baseName)
1595}
1596
1597var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1598
Paul Duffindef8a892020-05-08 15:36:30 +01001599type frameworkModulesNamingScheme struct {
1600}
1601
1602func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1603 suffix := scope.name
1604 if scope == apiScopeModuleLib {
1605 suffix = "module_libs_"
1606 }
1607 return suffix
1608}
1609
1610func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1611 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1612}
1613
1614func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1615 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1616}
1617
1618func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1619 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1620}
1621
1622var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1623
Anton Hansson0bd88d02020-05-25 12:20:51 +01001624func moduleStubLinkType(name string) (stub bool, ret linkType) {
1625 // This suffix-based approach is fragile and could potentially mis-trigger.
1626 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1627 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1628 return true, javaSdk
1629 }
1630 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1631 return true, javaSystem
1632 }
1633 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1634 return true, javaModule
1635 }
1636 if strings.HasSuffix(name, ".stubs.test") {
1637 return true, javaSystem
1638 }
1639 return false, javaPlatform
1640}
1641
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001642// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1643// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1644// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1645// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1646// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001647func SdkLibraryFactory() android.Module {
1648 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001649
1650 // Initialize information common between source and prebuilt.
1651 module.initCommon(&module.ModuleBase)
1652
Inseob Kimc0907f12019-02-08 21:00:45 +09001653 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001654 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001655 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001656
1657 // Initialize the map from scope to scope specific properties.
1658 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1659 for _, scope := range allApiScopes {
1660 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1661 }
1662 module.scopeToProperties = scopeToProperties
1663
Paul Duffin344c4ee2020-04-29 23:35:13 +01001664 // Add the properties containing visibility rules so that they are checked.
Paul Duffin9d582cc2020-05-16 15:52:12 +01001665 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001666 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1667 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1668
Paul Duffin1a724e62020-05-08 13:44:43 +01001669 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001670 // If no implementation is required then it cannot be used as a shared library
1671 // either.
1672 if !module.requiresRuntimeImplementationLibrary() {
1673 // If shared_library has been explicitly set to true then it is incompatible
1674 // with api_only: true.
1675 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1676 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1677 }
1678 // Set shared_library: false.
1679 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1680 }
1681
Paul Duffin1a724e62020-05-08 13:44:43 +01001682 if module.initCommonAfterDefaultsApplied(ctx) {
1683 module.CreateInternalModules(ctx)
1684 }
1685 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001686 return module
1687}
Colin Cross79c7c262019-04-17 11:11:46 -07001688
1689//
1690// SDK library prebuilts
1691//
1692
Paul Duffin56d44902020-01-31 13:36:25 +00001693// Properties associated with each api scope.
1694type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001695 Jars []string `android:"path"`
1696
1697 Sdk_version *string
1698
Colin Cross79c7c262019-04-17 11:11:46 -07001699 // List of shared java libs that this module has dependencies to
1700 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001701
Paul Duffin5fb82132020-04-29 20:45:27 +01001702 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001703 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001704
1705 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001706 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001707
1708 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001709 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001710}
1711
Paul Duffin56d44902020-01-31 13:36:25 +00001712type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001713 // List of shared java libs, common to all scopes, that this module has
1714 // dependencies to
1715 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001716}
1717
Paul Duffinf642a312020-06-12 17:46:39 +01001718type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001719 android.ModuleBase
1720 android.DefaultableModuleBase
1721 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001722 android.ApexModuleBase
1723 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001724
1725 properties sdkLibraryImportProperties
1726
Paul Duffin6a2bd112020-04-07 19:27:04 +01001727 // Map from api scope to the scope specific property structure.
1728 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1729
Paul Duffin56d44902020-01-31 13:36:25 +00001730 commonToSdkLibraryAndImport
Paul Duffinf642a312020-06-12 17:46:39 +01001731
1732 // The reference to the implementation library created by the source module.
1733 // Is nil if the source module does not exist.
1734 implLibraryModule *Library
1735
1736 // The reference to the xml permissions module created by the source module.
1737 // Is nil if the source module does not exist.
1738 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001739}
1740
Paul Duffinf642a312020-06-12 17:46:39 +01001741var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001742
Paul Duffin6a2bd112020-04-07 19:27:04 +01001743// The type of a structure that contains a field of type sdkLibraryScopeProperties
1744// for each apiscope in allApiScopes, e.g. something like:
1745// struct {
1746// Public sdkLibraryScopeProperties
1747// System sdkLibraryScopeProperties
1748// ...
1749// }
1750var allScopeStructType = createAllScopePropertiesStructType()
1751
1752// Dynamically create a structure type for each apiscope in allApiScopes.
1753func createAllScopePropertiesStructType() reflect.Type {
1754 var fields []reflect.StructField
1755 for _, apiScope := range allApiScopes {
1756 field := reflect.StructField{
1757 Name: apiScope.fieldName,
1758 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1759 }
1760 fields = append(fields, field)
1761 }
1762
1763 return reflect.StructOf(fields)
1764}
1765
1766// Create an instance of the scope specific structure type and return a map
1767// from apiscope to a pointer to each scope specific field.
1768func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1769 allScopePropertiesPtr := reflect.New(allScopeStructType)
1770 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1771 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1772
1773 for _, apiScope := range allApiScopes {
1774 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1775 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1776 }
1777
1778 return allScopePropertiesPtr.Interface(), scopeProperties
1779}
1780
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001781// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001782func sdkLibraryImportFactory() android.Module {
Paul Duffinf642a312020-06-12 17:46:39 +01001783 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001784
Paul Duffin6a2bd112020-04-07 19:27:04 +01001785 allScopeProperties, scopeToProperties := createPropertiesInstance()
1786 module.scopeProperties = scopeToProperties
1787 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001788
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001789 // Initialize information common between source and prebuilt.
1790 module.initCommon(&module.ModuleBase)
1791
Paul Duffin0bdcb272020-02-06 15:24:57 +00001792 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001793 android.InitApexModule(module)
1794 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001795 InitJavaModule(module, android.HostAndDeviceSupported)
1796
Paul Duffin1a724e62020-05-08 13:44:43 +01001797 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1798 if module.initCommonAfterDefaultsApplied(mctx) {
1799 module.createInternalModules(mctx)
1800 }
1801 })
Colin Cross79c7c262019-04-17 11:11:46 -07001802 return module
1803}
1804
Paul Duffinf642a312020-06-12 17:46:39 +01001805func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001806 return &module.prebuilt
1807}
1808
Paul Duffinf642a312020-06-12 17:46:39 +01001809func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001810 return module.prebuilt.Name(module.ModuleBase.Name())
1811}
1812
Paul Duffinf642a312020-06-12 17:46:39 +01001813func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001814
Paul Duffin50061512020-01-21 16:31:05 +00001815 // If the build is configured to use prebuilts then force this to be preferred.
1816 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1817 module.prebuilt.ForcePrefer()
1818 }
1819
Paul Duffin6a2bd112020-04-07 19:27:04 +01001820 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001821 if len(scopeProperties.Jars) == 0 {
1822 continue
1823 }
1824
Paul Duffinf6155722020-04-09 00:07:11 +01001825 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001826
Paul Duffin533f9c72020-05-20 16:18:00 +01001827 if len(scopeProperties.Stub_srcs) > 0 {
1828 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1829 }
Paul Duffin56d44902020-01-31 13:36:25 +00001830 }
Colin Cross79c7c262019-04-17 11:11:46 -07001831
1832 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1833 javaSdkLibrariesLock.Lock()
1834 defer javaSdkLibrariesLock.Unlock()
1835 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1836}
1837
Paul Duffinf642a312020-06-12 17:46:39 +01001838func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001839 // Creates a java import for the jar with ".stubs" suffix
1840 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001841 Name *string
1842 Sdk_version *string
1843 Libs []string
1844 Jars []string
1845 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001846 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001847 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001848 props.Sdk_version = scopeProperties.Sdk_version
1849 // Prepend any of the libs from the legacy public properties to the libs for each of the
1850 // scopes to avoid having to duplicate them in each scope.
1851 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1852 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001853
Paul Duffindd89a282020-05-13 16:08:09 +01001854 // The imports are preferred if the java_sdk_library_import is preferred.
1855 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001856
1857 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001858}
1859
Paul Duffinf642a312020-06-12 17:46:39 +01001860func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001861 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001862 Name *string
1863 Srcs []string
1864 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001865 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001866 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001867 props.Srcs = scopeProperties.Stub_srcs
1868 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001869
1870 // The stubs source is preferred if the java_sdk_library_import is preferred.
1871 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001872}
1873
Paul Duffinf642a312020-06-12 17:46:39 +01001874func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001875 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001876 if len(scopeProperties.Jars) == 0 {
1877 continue
1878 }
1879
1880 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001881 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001882
1883 if len(scopeProperties.Stub_srcs) > 0 {
1884 // Add dependencies to the prebuilt stubs source library
1885 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1886 }
Paul Duffin56d44902020-01-31 13:36:25 +00001887 }
Paul Duffinf642a312020-06-12 17:46:39 +01001888
1889 implName := module.implLibraryModuleName()
1890 if ctx.OtherModuleExists(implName) {
1891 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1892
1893 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1894 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1895 // Add dependency to the rule for generating the xml permissions file
1896 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1897 }
1898 }
Colin Cross79c7c262019-04-17 11:11:46 -07001899}
1900
Paul Duffinf642a312020-06-12 17:46:39 +01001901func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1902 depTag := mctx.OtherModuleDependencyTag(dep)
1903 if depTag == xmlPermissionsFileTag {
1904 return true
1905 }
1906
1907 // None of the other dependencies of the java_sdk_library_import are in the same apex
1908 // as the one that references this module.
1909 return false
1910}
1911
1912func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46fdda82020-05-14 15:39:10 +01001913 return module.commonOutputFiles(tag)
1914}
1915
Paul Duffinf642a312020-06-12 17:46:39 +01001916func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001917 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001918 ctx.VisitDirectDeps(func(to android.Module) {
1919 tag := ctx.OtherModuleDependencyTag(to)
1920
Paul Duffin533f9c72020-05-20 16:18:00 +01001921 // Extract information from any of the scope specific dependencies.
1922 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1923 apiScope := scopeTag.apiScope
1924 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1925
1926 // Extract information from the dependency. The exact information extracted
1927 // is determined by the nature of the dependency which is determined by the tag.
1928 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinf642a312020-06-12 17:46:39 +01001929 } else if tag == implLibraryTag {
1930 if implLibrary, ok := to.(*Library); ok {
1931 module.implLibraryModule = implLibrary
1932 } else {
1933 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1934 }
1935 } else if tag == xmlPermissionsFileTag {
1936 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1937 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1938 } else {
1939 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1940 }
Colin Cross79c7c262019-04-17 11:11:46 -07001941 }
1942 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001943
1944 // Populate the scope paths with information from the properties.
1945 for apiScope, scopeProperties := range module.scopeProperties {
1946 if len(scopeProperties.Jars) == 0 {
1947 continue
1948 }
1949
1950 paths := module.getScopePathsCreateIfNeeded(apiScope)
1951 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1952 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1953 }
Colin Cross79c7c262019-04-17 11:11:46 -07001954}
1955
Paul Duffinf642a312020-06-12 17:46:39 +01001956func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1957
1958 // For consistency with SdkLibrary make the implementation jar available to libraries that
1959 // are within the same APEX.
1960 implLibraryModule := module.implLibraryModule
1961 if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
1962 if headerJars {
1963 return implLibraryModule.HeaderJars()
1964 } else {
1965 return implLibraryModule.ImplementationJars()
1966 }
1967 }
1968
Paul Duffina3fb67d2020-05-20 14:20:02 +01001969 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001970}
1971
Colin Cross79c7c262019-04-17 11:11:46 -07001972// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01001973func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001974 // This module is just a wrapper for the prebuilt stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01001975 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07001976}
1977
1978// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01001979func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001980 // This module is just a wrapper for the stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01001981 return module.sdkJars(ctx, sdkVersion, false)
1982}
1983
1984// to satisfy apex.javaDependency interface
1985func (module *SdkLibraryImport) DexJar() android.Path {
1986 if module.implLibraryModule == nil {
1987 return nil
1988 } else {
1989 return module.implLibraryModule.DexJar()
1990 }
1991}
1992
1993// to satisfy apex.javaDependency interface
1994func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
1995 if module.implLibraryModule == nil {
1996 return nil
1997 } else {
1998 return module.implLibraryModule.JacocoReportClassesFile()
1999 }
2000}
2001
2002// to satisfy apex.javaDependency interface
2003func (module *SdkLibraryImport) Stem() string {
2004 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002005}
Jiyong Parke3833882020-02-17 17:28:10 +09002006
2007//
2008// java_sdk_library_xml
2009//
2010type sdkLibraryXml struct {
2011 android.ModuleBase
2012 android.DefaultableModuleBase
2013 android.ApexModuleBase
2014
2015 properties sdkLibraryXmlProperties
2016
2017 outputFilePath android.OutputPath
2018 installDirPath android.InstallPath
2019}
2020
2021type sdkLibraryXmlProperties struct {
2022 // canonical name of the lib
2023 Lib_name *string
2024}
2025
2026// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2027// Not to be used directly by users. java_sdk_library internally uses this.
2028func sdkLibraryXmlFactory() android.Module {
2029 module := &sdkLibraryXml{}
2030
2031 module.AddProperties(&module.properties)
2032
2033 android.InitApexModule(module)
2034 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2035
2036 return module
2037}
2038
2039// from android.PrebuiltEtcModule
2040func (module *sdkLibraryXml) SubDir() string {
2041 return "permissions"
2042}
2043
2044// from android.PrebuiltEtcModule
2045func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2046 return module.outputFilePath
2047}
2048
2049// from android.ApexModule
2050func (module *sdkLibraryXml) AvailableFor(what string) bool {
2051 return true
2052}
2053
2054func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2055 // do nothing
2056}
2057
2058// File path to the runtime implementation library
2059func (module *sdkLibraryXml) implPath() string {
2060 implName := proptools.String(module.properties.Lib_name)
2061 if apexName := module.ApexName(); apexName != "" {
2062 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
2063 // In most cases, this works fine. But when apex_name is set or override_apex is used
2064 // this can be wrong.
2065 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2066 }
2067 partition := "system"
2068 if module.SocSpecific() {
2069 partition = "vendor"
2070 } else if module.DeviceSpecific() {
2071 partition = "odm"
2072 } else if module.ProductSpecific() {
2073 partition = "product"
2074 } else if module.SystemExtSpecific() {
2075 partition = "system_ext"
2076 }
2077 return "/" + partition + "/framework/" + implName + ".jar"
2078}
2079
2080func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2081 libName := proptools.String(module.properties.Lib_name)
2082 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2083
2084 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2085 rule := android.NewRuleBuilder()
2086 rule.Command().
2087 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2088 Output(module.outputFilePath)
2089
2090 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2091
2092 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2093}
2094
2095func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2096 if !module.IsForPlatform() {
2097 return []android.AndroidMkEntries{android.AndroidMkEntries{
2098 Disabled: true,
2099 }}
2100 }
2101
2102 return []android.AndroidMkEntries{android.AndroidMkEntries{
2103 Class: "ETC",
2104 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2105 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2106 func(entries *android.AndroidMkEntries) {
2107 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2108 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2109 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2110 },
2111 },
2112 }}
2113}
Paul Duffin61871622020-02-10 13:37:10 +00002114
2115type sdkLibrarySdkMemberType struct {
2116 android.SdkMemberTypeBase
2117}
2118
2119func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2120 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2121}
2122
2123func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2124 _, ok := module.(*SdkLibrary)
2125 return ok
2126}
2127
2128func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2129 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2130}
2131
2132func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2133 return &sdkLibrarySdkMemberProperties{}
2134}
2135
2136type sdkLibrarySdkMemberProperties struct {
2137 android.SdkMemberPropertiesBase
2138
2139 // Scope to per scope properties.
2140 Scopes map[*apiScope]scopeProperties
2141
2142 // Additional libraries that the exported stubs libraries depend upon.
2143 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01002144
2145 // The Java stubs source files.
2146 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01002147
2148 // The naming scheme.
2149 Naming_scheme *string
Paul Duffina84756c2020-05-26 20:57:10 +01002150
2151 // True if the java_sdk_library_import is for a shared library, false
2152 // otherwise.
2153 Shared_library *bool
Paul Duffin61871622020-02-10 13:37:10 +00002154}
2155
2156type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01002157 Jars android.Paths
2158 StubsSrcJar android.Path
2159 CurrentApiFile android.Path
2160 RemovedApiFile android.Path
2161 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00002162}
2163
2164func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2165 sdk := variant.(*SdkLibrary)
2166
2167 s.Scopes = make(map[*apiScope]scopeProperties)
2168 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01002169 paths := sdk.findScopePaths(apiScope)
2170 if paths == nil {
2171 continue
2172 }
2173
Paul Duffin61871622020-02-10 13:37:10 +00002174 jars := paths.stubsImplPath
2175 if len(jars) > 0 {
2176 properties := scopeProperties{}
2177 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01002178 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01002179 properties.StubsSrcJar = paths.stubsSrcJar.Path()
2180 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2181 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffin61871622020-02-10 13:37:10 +00002182 s.Scopes[apiScope] = properties
2183 }
2184 }
2185
2186 s.Libs = sdk.properties.Libs
Paul Duffind11e78e2020-05-15 20:37:11 +01002187 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffina84756c2020-05-26 20:57:10 +01002188 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin61871622020-02-10 13:37:10 +00002189}
2190
2191func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01002192 if s.Naming_scheme != nil {
2193 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2194 }
Paul Duffina84756c2020-05-26 20:57:10 +01002195 if s.Shared_library != nil {
2196 propertySet.AddProperty("shared_library", *s.Shared_library)
2197 }
Paul Duffinf8e08b22020-05-13 16:54:55 +01002198
Paul Duffin61871622020-02-10 13:37:10 +00002199 for _, apiScope := range allApiScopes {
2200 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01002201 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00002202
Paul Duffinf488ef22020-04-09 00:10:17 +01002203 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2204
Paul Duffin61871622020-02-10 13:37:10 +00002205 var jars []string
2206 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01002207 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00002208 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2209 jars = append(jars, dest)
2210 }
2211 scopeSet.AddProperty("jars", jars)
2212
Paul Duffinf488ef22020-04-09 00:10:17 +01002213 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2214 // the source files are also unpacked.
2215 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2216 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2217 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2218
Paul Duffin75dcc802020-04-09 01:08:11 +01002219 if properties.CurrentApiFile != nil {
2220 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2221 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2222 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2223 }
2224
2225 if properties.RemovedApiFile != nil {
2226 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffinb1787352020-06-02 13:00:02 +01002227 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin75dcc802020-04-09 01:08:11 +01002228 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2229 }
2230
Paul Duffin61871622020-02-10 13:37:10 +00002231 if properties.SdkVersion != "" {
2232 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2233 }
2234 }
2235 }
2236
2237 if len(s.Libs) > 0 {
2238 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2239 }
2240}