blob: 7a24a0160788b5323ef84d92d314dbad3e34d90e [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"
Jiyong Park82484c02018-04-23 21:41:26 +090022 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090023 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025
Paul Duffind1b3a922020-01-22 11:57:20 +000026 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027 "github.com/google/blueprint/proptools"
Paul Duffin6a2bd112020-04-07 19:27:04 +010028
29 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030)
31
Jooyung Han58f26ab2019-12-18 15:34:32 +090032const (
Paul Duffin1c094a02020-05-08 15:52:37 +010033 sdkXmlFileSuffix = ".xml"
34 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090035 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
36 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090037 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090038 ` you may not use this file except in compliance with the License.\n` +
39 ` You may obtain a copy of the License at\n` +
40 `\n` +
41 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
42 `\n` +
43 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090044 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090045 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
46 ` See the License for the specific language governing permissions and\n` +
47 ` limitations under the License.\n` +
48 `-->\n` +
49 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090050 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090051 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090052)
53
Paul Duffind1b3a922020-01-22 11:57:20 +000054// A tag to associated a dependency with a specific api scope.
55type scopeDependencyTag struct {
56 blueprint.BaseDependencyTag
57 name string
58 apiScope *apiScope
Paul Duffin5fb82132020-04-29 20:45:27 +010059
60 // Function for extracting appropriate path information from the dependency.
61 depInfoExtractor func(paths *scopePaths, dep android.Module) error
62}
63
64// Extract tag specific information from the dependency.
65func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
66 err := tag.depInfoExtractor(paths, dep)
67 if err != nil {
68 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
69 }
Paul Duffind1b3a922020-01-22 11:57:20 +000070}
71
72// Provides information about an api scope, e.g. public, system, test.
73type apiScope struct {
74 // The name of the api scope, e.g. public, system, test
75 name string
76
Paul Duffin51a2bee2020-05-05 14:40:52 +010077 // The api scope that this scope extends.
78 extends *apiScope
79
Paul Duffin3a254982020-04-28 10:44:03 +010080 // The legacy enabled status for a specific scope can be dependent on other
81 // properties that have been specified on the library so it is provided by
82 // a function that can determine the status by examining those properties.
83 legacyEnabledStatus func(module *SdkLibrary) bool
84
85 // The default enabled status for non-legacy behavior, which is triggered by
86 // explicitly enabling at least one api scope.
87 defaultEnabledStatus bool
88
89 // Gets a pointer to the scope specific properties.
90 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
91
Paul Duffin6a2bd112020-04-07 19:27:04 +010092 // The name of the field in the dynamically created structure.
93 fieldName string
94
Paul Duffin0f270632020-05-13 19:19:49 +010095 // The name of the property in the java_sdk_library_import
96 propertyName string
97
Paul Duffind1b3a922020-01-22 11:57:20 +000098 // The tag to use to depend on the stubs library module.
99 stubsTag scopeDependencyTag
100
Paul Duffina377e4c2020-04-29 13:30:54 +0100101 // The tag to use to depend on the stubs source module (if separate from the API module).
102 stubsSourceTag scopeDependencyTag
103
104 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
105 apiFileTag scopeDependencyTag
106
Paul Duffin5fb82132020-04-29 20:45:27 +0100107 // The tag to use to depend on the stubs source and API module.
108 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000109
110 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
111 apiFilePrefix string
112
113 // The scope specific prefix to add to the sdk library module name to construct a scope specific
114 // module name.
115 moduleSuffix string
116
Paul Duffind1b3a922020-01-22 11:57:20 +0000117 // SDK version that the stubs library is built against. Note that this is always
118 // *current. Older stubs library built with a numbered SDK version is created from
119 // the prebuilt jar.
120 sdkVersion string
Paul Duffin3c7c3472020-04-07 18:50:10 +0100121
122 // Extra arguments to pass to droidstubs for this scope.
123 droidstubsArgs []string
Anton Hansson5ff28e52020-05-02 11:19:36 +0100124
Paul Duffina377e4c2020-04-29 13:30:54 +0100125 // The args that must be passed to droidstubs to generate the stubs source
126 // for this scope.
127 //
128 // The stubs source must include the definitions of everything that is in this
129 // api scope and all the scopes that this one extends.
130 droidstubsArgsForGeneratingStubsSource []string
131
132 // The args that must be passed to droidstubs to generate the API for this scope.
133 //
134 // The API only includes the additional members that this scope adds over the scope
135 // that it extends.
136 droidstubsArgsForGeneratingApi []string
137
138 // True if the stubs source and api can be created by the same metalava invocation.
139 createStubsSourceAndApiTogether bool
140
Anton Hansson5ff28e52020-05-02 11:19:36 +0100141 // Whether the api scope can be treated as unstable, and should skip compat checks.
142 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000143}
144
145// Initialize a scope, creating and adding appropriate dependency tags
146func initApiScope(scope *apiScope) *apiScope {
Paul Duffin5fb82132020-04-29 20:45:27 +0100147 name := scope.name
Paul Duffin0f270632020-05-13 19:19:49 +0100148 scope.propertyName = strings.ReplaceAll(name, "-", "_")
149 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000150 scope.stubsTag = scopeDependencyTag{
Paul Duffin5fb82132020-04-29 20:45:27 +0100151 name: name + "-stubs",
152 apiScope: scope,
153 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000154 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100155 scope.stubsSourceTag = scopeDependencyTag{
156 name: name + "-stubs-source",
157 apiScope: scope,
158 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
159 }
160 scope.apiFileTag = scopeDependencyTag{
161 name: name + "-api",
162 apiScope: scope,
163 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
164 }
Paul Duffin5fb82132020-04-29 20:45:27 +0100165 scope.stubsSourceAndApiTag = scopeDependencyTag{
166 name: name + "-stubs-source-and-api",
167 apiScope: scope,
168 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000169 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100170
171 // To get the args needed to generate the stubs source append all the args from
172 // this scope and all the scopes it extends as each set of args adds additional
173 // members to the stubs.
174 var stubsSourceArgs []string
175 for s := scope; s != nil; s = s.extends {
176 stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
177 }
178 scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
179
180 // Currently the args needed to generate the API are the same as the args
181 // needed to add additional members.
182 apiArgs := scope.droidstubsArgs
183 scope.droidstubsArgsForGeneratingApi = apiArgs
184
185 // If the args needed to generate the stubs and API are the same then they
186 // can be generated in a single invocation of metalava, otherwise they will
187 // need separate invocations.
188 scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
189
Paul Duffind1b3a922020-01-22 11:57:20 +0000190 return scope
191}
192
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100193func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100194 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000195}
196
Paul Duffin5fb82132020-04-29 20:45:27 +0100197func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100198 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000199}
200
Paul Duffina377e4c2020-04-29 13:30:54 +0100201func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100202 return baseName + ".api" + scope.moduleSuffix
Paul Duffina377e4c2020-04-29 13:30:54 +0100203}
204
Paul Duffin3a254982020-04-28 10:44:03 +0100205func (scope *apiScope) String() string {
206 return scope.name
207}
208
Paul Duffind1b3a922020-01-22 11:57:20 +0000209type apiScopes []*apiScope
210
211func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
212 var list []string
213 for _, scope := range scopes {
214 list = append(list, accessor(scope))
215 }
216 return list
217}
218
Jiyong Parkc678ad32018-04-10 13:07:10 +0900219var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000220 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100221 name: "public",
222
223 // Public scope is enabled by default for both legacy and non-legacy modes.
224 legacyEnabledStatus: func(module *SdkLibrary) bool {
225 return true
226 },
227 defaultEnabledStatus: true,
228
229 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
230 return &module.sdkLibraryProperties.Public
231 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000232 sdkVersion: "current",
233 })
234 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100235 name: "system",
236 extends: apiScopePublic,
237 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
238 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
239 return &module.sdkLibraryProperties.System
240 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100241 apiFilePrefix: "system-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100242 moduleSuffix: ".system",
Anton Hanssone366fff2020-04-28 16:47:41 +0100243 sdkVersion: "system_current",
Paul Duffin991f2622020-04-29 22:18:41 +0100244 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000245 })
246 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100247 name: "test",
248 extends: apiScopePublic,
249 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
250 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
251 return &module.sdkLibraryProperties.Test
252 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100253 apiFilePrefix: "test-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100254 moduleSuffix: ".test",
Anton Hanssone366fff2020-04-28 16:47:41 +0100255 sdkVersion: "test_current",
256 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100257 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000258 })
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100259 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin0f270632020-05-13 19:19:49 +0100260 name: "module-lib",
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100261 extends: apiScopeSystem,
262 // Module_lib scope is disabled by default in legacy mode.
263 //
264 // Enabling this would break existing usages.
265 legacyEnabledStatus: func(module *SdkLibrary) bool {
266 return false
267 },
268 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
269 return &module.sdkLibraryProperties.Module_lib
270 },
271 apiFilePrefix: "module-lib-",
272 moduleSuffix: ".module_lib",
273 sdkVersion: "module_current",
274 droidstubsArgs: []string{
275 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
276 },
277 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000278 allApiScopes = apiScopes{
279 apiScopePublic,
280 apiScopeSystem,
281 apiScopeTest,
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100282 apiScopeModuleLib,
Paul Duffind1b3a922020-01-22 11:57:20 +0000283 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900284)
285
Jiyong Park82484c02018-04-23 21:41:26 +0900286var (
287 javaSdkLibrariesLock sync.Mutex
288)
289
Jiyong Parkc678ad32018-04-10 13:07:10 +0900290// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900291// 1) disallowing linking to the runtime shared lib
292// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900293
294func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000295 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900296
Jiyong Park82484c02018-04-23 21:41:26 +0900297 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
298 javaSdkLibraries := javaSdkLibraries(ctx.Config())
299 sort.Strings(*javaSdkLibraries)
300 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
301 })
Paul Duffin61871622020-02-10 13:37:10 +0000302
303 // Register sdk member types.
304 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
305 android.SdkMemberTypeBase{
306 PropertyName: "java_sdk_libs",
307 SupportsSdk: true,
308 },
309 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900310}
311
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000312func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
313 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
314 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
315}
316
Paul Duffin3a254982020-04-28 10:44:03 +0100317// Properties associated with each api scope.
318type ApiScopeProperties struct {
319 // Indicates whether the api surface is generated.
320 //
321 // If this is set for any scope then all scopes must explicitly specify if they
322 // are enabled. This is to prevent new usages from depending on legacy behavior.
323 //
324 // Otherwise, if this is not set for any scope then the default behavior is
325 // scope specific so please refer to the scope specific property documentation.
326 Enabled *bool
Paul Duffin080f5ee2020-05-12 11:50:28 +0100327
328 // The sdk_version to use for building the stubs.
329 //
330 // If not specified then it will use an sdk_version determined as follows:
331 // 1) If the sdk_version specified on the java_sdk_library is none then this
332 // will be none. This is used for java_sdk_library instances that are used
333 // to create stubs that contribute to the core_current sdk version.
334 // 2) Otherwise, it is assumed that this library extends but does not contribute
335 // directly to a specific sdk_version and so this uses the sdk_version appropriate
336 // for the api scope. e.g. public will use sdk_version: current, system will use
337 // sdk_version: system_current, etc.
338 //
339 // This does not affect the sdk_version used for either generating the stubs source
340 // or the API file. They both have to use the same sdk_version as is used for
341 // compiling the implementation library.
342 Sdk_version *string
Paul Duffin3a254982020-04-28 10:44:03 +0100343}
344
Jiyong Parkc678ad32018-04-10 13:07:10 +0900345type sdkLibraryProperties struct {
Paul Duffin344c4ee2020-04-29 23:35:13 +0100346 // Visibility for stubs library modules. If not specified then defaults to the
347 // visibility property.
348 Stubs_library_visibility []string
349
350 // Visibility for stubs source modules. If not specified then defaults to the
351 // visibility property.
352 Stubs_source_visibility []string
353
Sundong Ahnf043cf62018-06-25 16:04:37 +0900354 // List of Java libraries that will be in the classpath when building stubs
355 Stub_only_libs []string `android:"arch_variant"`
356
Paul Duffin7a586d32019-12-30 17:09:34 +0000357 // list of package names that will be documented and publicized as API.
358 // This allows the API to be restricted to a subset of the source files provided.
359 // If this is unspecified then all the source files will be treated as being part
360 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900361 Api_packages []string
362
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900363 // list of package names that must be hidden from the API
364 Hidden_api_packages []string
365
Paul Duffin749f98f2019-12-30 17:23:46 +0000366 // the relative path to the directory containing the api specification files.
367 // Defaults to "api".
368 Api_dir *string
369
Paul Duffin43db9be2019-12-30 17:35:49 +0000370 // If set to true there is no runtime library.
371 Api_only *bool
372
Paul Duffin11512472019-02-11 15:55:17 +0000373 // local files that are used within user customized droiddoc options.
374 Droiddoc_option_files []string
375
376 // additional droiddoc options
377 // Available variables for substitution:
378 //
379 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900380 Droiddoc_options []string
381
Paul Duffin2ce1e812020-05-20 19:35:27 +0100382 // is set to true, Metalava will allow framework SDK to contain annotations.
383 Annotations_enabled *bool
384
Sundong Ahn054b19a2018-10-19 13:46:09 +0900385 // a list of top-level directories containing files to merge qualifier annotations
386 // (i.e. those intended to be included in the stubs written) from.
387 Merge_annotations_dirs []string
388
389 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
390 Merge_inclusion_annotations_dirs []string
391
392 // If set to true, the path of dist files is apistubs/core. Defaults to false.
393 Core_lib *bool
394
Sundong Ahn80a87b32019-05-13 15:02:50 +0900395 // don't create dist rules.
396 No_dist *bool `blueprint:"mutated"`
397
Paul Duffin3a254982020-04-28 10:44:03 +0100398 // indicates whether system and test apis should be generated.
399 Generate_system_and_test_apis bool `blueprint:"mutated"`
400
401 // The properties specific to the public api scope
402 //
403 // Unless explicitly specified by using public.enabled the public api scope is
404 // enabled by default in both legacy and non-legacy mode.
405 Public ApiScopeProperties
406
407 // The properties specific to the system api scope
408 //
409 // In legacy mode the system api scope is enabled by default when sdk_version
410 // is set to something other than "none".
411 //
412 // In non-legacy mode the system api scope is disabled by default.
413 System ApiScopeProperties
414
415 // The properties specific to the test api scope
416 //
417 // In legacy mode the test api scope is enabled by default when sdk_version
418 // is set to something other than "none".
419 //
420 // In non-legacy mode the test api scope is disabled by default.
421 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000422
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100423 // The properties specific to the module_lib api scope
424 //
425 // Unless explicitly specified by using test.enabled the module_lib api scope is
426 // disabled by default.
427 Module_lib ApiScopeProperties
428
Paul Duffin8986cc92020-05-10 19:32:20 +0100429 // Properties related to api linting.
430 Api_lint struct {
431 // Enable api linting.
432 Enabled *bool
433 }
434
Jiyong Parkc678ad32018-04-10 13:07:10 +0900435 // TODO: determines whether to create HTML doc or not
436 //Html_doc *bool
437}
438
Paul Duffind1b3a922020-01-22 11:57:20 +0000439type scopePaths struct {
Paul Duffin75dcc802020-04-09 01:08:11 +0100440 stubsHeaderPath android.Paths
441 stubsImplPath android.Paths
442 currentApiFilePath android.Path
443 removedApiFilePath android.Path
444 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000445}
446
Paul Duffin5fb82132020-04-29 20:45:27 +0100447func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
448 if lib, ok := dep.(Dependency); ok {
449 paths.stubsHeaderPath = lib.HeaderJars()
450 paths.stubsImplPath = lib.ImplementationJars()
451 return nil
452 } else {
453 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
454 }
455}
456
Paul Duffina377e4c2020-04-29 13:30:54 +0100457func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
458 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
459 action(apiStubsProvider)
Paul Duffin5fb82132020-04-29 20:45:27 +0100460 return nil
461 } else {
462 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
463 }
464}
465
Paul Duffina377e4c2020-04-29 13:30:54 +0100466func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
467 paths.currentApiFilePath = provider.ApiFilePath()
468 paths.removedApiFilePath = provider.RemovedApiFilePath()
469}
470
471func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
472 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
473 paths.extractApiInfoFromApiStubsProvider(provider)
474 })
475}
476
477func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsProvider) {
478 paths.stubsSrcJar = provider.StubsSrcJar()
479}
480
481func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
482 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
483 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
484 })
485}
486
487func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
488 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
489 paths.extractApiInfoFromApiStubsProvider(provider)
490 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
491 })
492}
493
494type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1a724e62020-05-08 13:44:43 +0100495 // The naming scheme to use for the components that this module creates.
496 //
Paul Duffindef8a892020-05-08 15:36:30 +0100497 // If not specified then it defaults to "default". The other allowable value is
498 // "framework-modules" which matches the scheme currently used by framework modules
499 // for the equivalent components represented as separate Soong modules.
Paul Duffin1a724e62020-05-08 13:44:43 +0100500 //
501 // This is a temporary mechanism to simplify conversion from separate modules for each
502 // component that follow a different naming pattern to the default one.
503 //
504 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffina377e4c2020-04-29 13:30:54 +0100505 Naming_scheme *string
506}
507
Paul Duffin56d44902020-01-31 13:36:25 +0000508// Common code between sdk library and sdk library import
509type commonToSdkLibraryAndImport struct {
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100510 moduleBase *android.ModuleBase
511
Paul Duffin56d44902020-01-31 13:36:25 +0000512 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100513
514 namingScheme sdkLibraryComponentNamingScheme
515
516 commonProperties commonToSdkLibraryAndImportProperties
Paul Duffin56d44902020-01-31 13:36:25 +0000517}
518
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100519func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
520 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100521
522 moduleBase.AddProperties(&c.commonProperties)
523}
524
525func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
526 schemeProperty := proptools.StringDefault(c.commonProperties.Naming_scheme, "default")
527 switch schemeProperty {
528 case "default":
529 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100530 case "framework-modules":
531 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100532 default:
533 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
534 return false
535 }
536
537 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100538}
539
540// Name of the java_library module that compiles the stubs source.
541func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100542 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100543}
544
545// Name of the droidstubs module that generates the stubs source and may also
546// generate/check the API.
547func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100548 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100549}
550
551// Name of the droidstubs module that generates/checks the API. Only used if it
552// requires different arts to the stubs source generating module.
553func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100554 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100555}
556
Paul Duffin56d44902020-01-31 13:36:25 +0000557func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
558 if c.scopePaths == nil {
559 c.scopePaths = make(map[*apiScope]*scopePaths)
560 }
561 paths := c.scopePaths[scope]
562 if paths == nil {
563 paths = &scopePaths{}
564 c.scopePaths[scope] = paths
565 }
566
567 return paths
568}
569
Inseob Kimc0907f12019-02-08 21:00:45 +0900570type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900571 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900572
Sundong Ahn054b19a2018-10-19 13:46:09 +0900573 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900574
Paul Duffin3a254982020-04-28 10:44:03 +0100575 // Map from api scope to the scope specific property structure.
576 scopeToProperties map[*apiScope]*ApiScopeProperties
577
Paul Duffin56d44902020-01-31 13:36:25 +0000578 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900579}
580
Inseob Kimc0907f12019-02-08 21:00:45 +0900581var _ Dependency = (*SdkLibrary)(nil)
582var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800583
Paul Duffin3a254982020-04-28 10:44:03 +0100584func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
585 return module.sdkLibraryProperties.Generate_system_and_test_apis
586}
587
588func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
589 // Check to see if any scopes have been explicitly enabled. If any have then all
590 // must be.
591 anyScopesExplicitlyEnabled := false
592 for _, scope := range allApiScopes {
593 scopeProperties := module.scopeToProperties[scope]
594 if scopeProperties.Enabled != nil {
595 anyScopesExplicitlyEnabled = true
596 break
597 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000598 }
Paul Duffin3a254982020-04-28 10:44:03 +0100599
600 var generatedScopes apiScopes
601 enabledScopes := make(map[*apiScope]struct{})
602 for _, scope := range allApiScopes {
603 scopeProperties := module.scopeToProperties[scope]
604 // If any scopes are explicitly enabled then ignore the legacy enabled status.
605 // This is to ensure that any new usages of this module type do not rely on legacy
606 // behaviour.
607 defaultEnabledStatus := false
608 if anyScopesExplicitlyEnabled {
609 defaultEnabledStatus = scope.defaultEnabledStatus
610 } else {
611 defaultEnabledStatus = scope.legacyEnabledStatus(module)
612 }
613 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
614 if enabled {
615 enabledScopes[scope] = struct{}{}
616 generatedScopes = append(generatedScopes, scope)
617 }
618 }
619
620 // Now check to make sure that any scope that is extended by an enabled scope is also
621 // enabled.
622 for _, scope := range allApiScopes {
623 if _, ok := enabledScopes[scope]; ok {
624 extends := scope.extends
625 if extends != nil {
626 if _, ok := enabledScopes[extends]; !ok {
627 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
628 }
629 }
630 }
631 }
632
633 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000634}
635
Paul Duffine74ac732020-02-06 13:51:46 +0000636var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
637
Jiyong Parke3833882020-02-17 17:28:10 +0900638func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
639 if dt, ok := depTag.(dependencyTag); ok {
640 return dt == xmlPermissionsFileTag
641 }
642 return false
643}
644
Inseob Kimc0907f12019-02-08 21:00:45 +0900645func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100646 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000647 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100648 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000649
Paul Duffina377e4c2020-04-29 13:30:54 +0100650 // If the stubs source and API cannot be generated together then add an additional dependency on
651 // the API module.
652 if apiScope.createStubsSourceAndApiTogether {
653 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100654 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100655 } else {
656 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100657 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
658 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100659 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900660 }
661
Paul Duffine74ac732020-02-06 13:51:46 +0000662 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
663 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900664 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000665 }
666
Sundong Ahn054b19a2018-10-19 13:46:09 +0900667 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900668}
669
Inseob Kimc0907f12019-02-08 21:00:45 +0900670func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000671 // Don't build an implementation library if this is api only.
672 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
673 module.Library.GenerateAndroidBuildActions(ctx)
674 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900675
Sundong Ahn57368eb2018-07-06 11:20:23 +0900676 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000677 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900678 // the recorded paths will be returned depending on the link type of the caller.
679 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900680 tag := ctx.OtherModuleDependencyTag(to)
681
Paul Duffin5fb82132020-04-29 20:45:27 +0100682 // Extract information from any of the scope specific dependencies.
683 if scopeTag, ok := tag.(scopeDependencyTag); ok {
684 apiScope := scopeTag.apiScope
685 scopePaths := module.getScopePaths(apiScope)
686
687 // Extract information from the dependency. The exact information extracted
688 // is determined by the nature of the dependency which is determined by the tag.
689 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900690 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900691 })
692}
693
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900694func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000695 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
696 return nil
697 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900698 entriesList := module.Library.AndroidMkEntries()
699 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700700 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900701 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900702}
703
Jiyong Parkc678ad32018-04-10 13:07:10 +0900704// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900705func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900706 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900707}
708
Jiyong Parkc678ad32018-04-10 13:07:10 +0900709// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900710func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900711 return module.BaseModuleName() + sdkXmlFileSuffix
712}
713
Anton Hansson6bb88102020-03-27 19:43:19 +0000714// The dist path of the stub artifacts
715func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
716 if module.ModuleBase.Owner() != "" {
717 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
718 } else if Bool(module.sdkLibraryProperties.Core_lib) {
719 return path.Join("apistubs", "core", apiScope.name)
720 } else {
721 return path.Join("apistubs", "android", apiScope.name)
722 }
723}
724
Paul Duffin12ceb462019-12-24 20:31:31 +0000725// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +0100726func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +0100727 scopeProperties := module.scopeToProperties[apiScope]
728 if scopeProperties.Sdk_version != nil {
729 return proptools.String(scopeProperties.Sdk_version)
730 }
731
Paul Duffin12ceb462019-12-24 20:31:31 +0000732 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
733 if sdkDep.hasStandardLibs() {
734 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000735 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000736 } else {
737 // Otherwise, use no system module.
738 return "none"
739 }
740}
741
Paul Duffind1b3a922020-01-22 11:57:20 +0000742func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
743 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900744}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900745
Paul Duffind1b3a922020-01-22 11:57:20 +0000746func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
747 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900748}
749
750// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +0100751func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900752 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +0100753 Name *string
754 Visibility []string
755 Srcs []string
756 Installable *bool
757 Sdk_version *string
758 System_modules *string
759 Patch_module *string
760 Libs []string
761 Compile_dex *bool
762 Java_version *string
763 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900764 Pdk struct {
765 Enabled *bool
766 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900767 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900768 Openjdk9 struct {
769 Srcs []string
770 Javacflags []string
771 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000772 Dist struct {
773 Targets []string
774 Dest *string
775 Dir *string
776 Tag *string
777 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900778 }{}
779
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100780 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +0100781
782 // If stubs_library_visibility is not set then the created module will use the
783 // visibility of this module.
784 visibility := module.sdkLibraryProperties.Stubs_library_visibility
785 props.Visibility = visibility
786
Jiyong Parkc678ad32018-04-10 13:07:10 +0900787 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100788 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000789 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100790 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +0100791 props.System_modules = module.deviceProperties.System_modules
792 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000793 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900794 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +0100795 // The stub-annotations library contains special versions of the annotations
796 // with CLASS retention policy, so that they're kept.
797 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
798 props.Libs = append(props.Libs, "stub-annotations")
799 }
Jiyong Park82484c02018-04-23 21:41:26 +0900800 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +0100801 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
802 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
803 props.Java_version = module.properties.Java_version
804 if module.deviceProperties.Compile_dex != nil {
805 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900806 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900807
Anton Hansson6bb88102020-03-27 19:43:19 +0000808 // Dist the class jar artifact for sdk builds.
809 if !Bool(module.sdkLibraryProperties.No_dist) {
810 props.Dist.Targets = []string{"sdk", "win_sdk"}
811 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
812 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
813 props.Dist.Tag = proptools.StringPtr(".jar")
814 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900815
Colin Cross84dfc3d2019-09-25 11:33:01 -0700816 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900817}
818
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100819// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +0100820// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +0100821func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900822 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900823 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +0100824 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900825 Srcs []string
826 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100827 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000828 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900829 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000830 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900831 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900832 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +0100833 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +0900834 Merge_annotations_dirs []string
835 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +0100836 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +0900837 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900838 Current ApiToCheck
839 Last_released ApiToCheck
840 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +0100841
842 Api_lint struct {
843 Enabled *bool
844 New_since *string
845 Baseline_file *string
846 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900847 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900848 Aidl struct {
849 Include_dirs []string
850 Local_include_dirs []string
851 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000852 Dist struct {
853 Targets []string
854 Dest *string
855 Dir *string
856 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900857 }{}
858
Paul Duffinda364252020-04-28 14:08:32 +0100859 // The stubs source processing uses the same compile time classpath when extracting the
860 // API from the implementation library as it does when compiling it. i.e. the same
861 // * sdk version
862 // * system_modules
863 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100864
Paul Duffina377e4c2020-04-29 13:30:54 +0100865 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +0100866
867 // If stubs_source_visibility is not set then the created module will use the
868 // visibility of this module.
869 visibility := module.sdkLibraryProperties.Stubs_source_visibility
870 props.Visibility = visibility
871
Paul Duffinc5d954a2020-05-16 18:54:24 +0100872 props.Srcs = append(props.Srcs, module.properties.Srcs...)
873 props.Sdk_version = module.deviceProperties.Sdk_version
874 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900875 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900876 // A droiddoc module has only one Libs property and doesn't distinguish between
877 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +0100878 props.Libs = module.properties.Libs
879 props.Libs = append(props.Libs, module.properties.Static_libs...)
880 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
881 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
882 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900883
Paul Duffin2ce1e812020-05-20 19:35:27 +0100884 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +0900885 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
886 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
887
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100888 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000889 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100890 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000891 }
892 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100893 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000894 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
895 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100896 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000897 disabledWarnings := []string{
898 "MissingPermission",
899 "BroadcastBehavior",
900 "HiddenSuperclass",
901 "DeprecationMismatch",
902 "UnavailableSymbol",
903 "SdkConstant",
904 "HiddenTypeParameter",
905 "Todo",
906 "Typo",
907 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100908 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900909
Paul Duffina377e4c2020-04-29 13:30:54 +0100910 if !createStubSources {
911 // Stubs are not required.
912 props.Generate_stubs = proptools.BoolPtr(false)
913 }
914
Paul Duffin3c7c3472020-04-07 18:50:10 +0100915 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +0100916 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000917 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100918 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900919
Paul Duffina377e4c2020-04-29 13:30:54 +0100920 if createApi {
921 // List of APIs identified from the provided source files are created. They are later
922 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
923 // last-released (a.k.a numbered) list of API.
924 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
925 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
926 apiDir := module.getApiDir()
927 currentApiFileName = path.Join(apiDir, currentApiFileName)
928 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900929
Paul Duffina377e4c2020-04-29 13:30:54 +0100930 // check against the not-yet-release API
931 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
932 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900933
Paul Duffina377e4c2020-04-29 13:30:54 +0100934 if !apiScope.unstable {
935 // check against the latest released API
936 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
937 props.Check_api.Last_released.Api_file = latestApiFilegroupName
938 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
939 module.latestRemovedApiFilegroupName(apiScope))
940 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +0100941
Paul Duffina377e4c2020-04-29 13:30:54 +0100942 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
943 // Enable api lint.
944 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
945 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +0100946
Paul Duffina377e4c2020-04-29 13:30:54 +0100947 // If it exists then pass a lint-baseline.txt through to droidstubs.
948 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
949 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
950 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
951 if err != nil {
952 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
953 }
954 if len(paths) == 1 {
955 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
956 } else if len(paths) != 0 {
957 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
958 }
Paul Duffin8986cc92020-05-10 19:32:20 +0100959 }
960 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900961
Paul Duffina377e4c2020-04-29 13:30:54 +0100962 // Dist the api txt artifact for sdk builds.
963 if !Bool(module.sdkLibraryProperties.No_dist) {
964 props.Dist.Targets = []string{"sdk", "win_sdk"}
965 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
966 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
967 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000968 }
969
Colin Cross84dfc3d2019-09-25 11:33:01 -0700970 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900971}
972
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900973func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
974 depTag := mctx.OtherModuleDependencyTag(dep)
975 if depTag == xmlPermissionsFileTag {
976 return true
977 }
978 return module.Library.DepIsInSameApex(mctx, dep)
979}
980
Jiyong Parkc678ad32018-04-10 13:07:10 +0900981// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +0100982func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900983 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +0100984 Name *string
985 Lib_name *string
986 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900987 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900988 Name: proptools.StringPtr(module.xmlFileName()),
989 Lib_name: proptools.StringPtr(module.BaseModuleName()),
990 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900991 }
Jiyong Parke3833882020-02-17 17:28:10 +0900992
Jiyong Parke3833882020-02-17 17:28:10 +0900993 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900994}
995
Paul Duffin50061512020-01-21 16:31:05 +0000996func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900997 var ver sdkVersion
998 var kind sdkKind
999 if s.usePrebuilt(ctx) {
1000 ver = s.version
1001 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001002 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001003 // We don't have prebuilt SDK for the specific sdkVersion.
1004 // Instead of breaking the build, fallback to use "system_current"
1005 ver = sdkVersionCurrent
1006 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001007 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001008
1009 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001010 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001011 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001012 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001013 if ctx.Config().AllowMissingDependencies() {
1014 return android.Paths{android.PathForSource(ctx, jar)}
1015 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001016 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001017 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001018 return nil
1019 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001020 return android.Paths{jarPath.Path()}
1021}
1022
Paul Duffind1b3a922020-01-22 11:57:20 +00001023func (module *SdkLibrary) sdkJars(
1024 ctx android.BaseModuleContext,
1025 sdkVersion sdkSpec,
1026 headerJars bool) android.Paths {
1027
Paul Duffin50061512020-01-21 16:31:05 +00001028 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1029 if sdkVersion.version.isNumbered() {
1030 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001031 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +00001032 if !sdkVersion.specified() {
1033 if headerJars {
Paul Duffinc5d954a2020-05-16 18:54:24 +01001034 return module.HeaderJars()
Paul Duffind1b3a922020-01-22 11:57:20 +00001035 } else {
Paul Duffinc5d954a2020-05-16 18:54:24 +01001036 return module.ImplementationJars()
Paul Duffind1b3a922020-01-22 11:57:20 +00001037 }
1038 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001039 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +09001040 switch sdkVersion.kind {
1041 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +00001042 apiScope = apiScopeSystem
1043 case sdkTest:
1044 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +09001045 case sdkPrivate:
Paul Duffinc5d954a2020-05-16 18:54:24 +01001046 return module.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +09001047 default:
Paul Duffin726d23c2020-01-22 16:30:37 +00001048 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +00001049 }
1050
Paul Duffin726d23c2020-01-22 16:30:37 +00001051 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +00001052 if headerJars {
1053 return paths.stubsHeaderPath
1054 } else {
1055 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +09001056 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001057 }
1058}
1059
Sundong Ahn241cd372018-07-13 16:16:44 +09001060// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001061func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1062 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1063}
1064
1065// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001066func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001067 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001068}
1069
Sundong Ahn80a87b32019-05-13 15:02:50 +09001070func (module *SdkLibrary) SetNoDist() {
1071 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1072}
1073
Colin Cross571cccf2019-02-04 11:22:08 -08001074var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1075
Jiyong Park82484c02018-04-23 21:41:26 +09001076func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001077 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001078 return &[]string{}
1079 }).(*[]string)
1080}
1081
Paul Duffin749f98f2019-12-30 17:23:46 +00001082func (module *SdkLibrary) getApiDir() string {
1083 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1084}
1085
Jiyong Parkc678ad32018-04-10 13:07:10 +09001086// For a java_sdk_library module, create internal modules for stubs, docs,
1087// runtime libs and xml file. If requested, the stubs and docs are created twice
1088// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001089func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1090 // If the module has been disabled then don't create any child modules.
1091 if !module.Enabled() {
1092 return
1093 }
1094
Paul Duffinc5d954a2020-05-16 18:54:24 +01001095 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001096 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001097 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001098 }
1099
Paul Duffin37e0b772019-12-30 17:20:10 +00001100 // If this builds against standard libraries (i.e. is not part of the core libraries)
1101 // then assume it provides both system and test apis. Otherwise, assume it does not and
1102 // also assume it does not contribute to the dist build.
1103 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1104 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001105 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001106 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1107
Inseob Kim8098faa2019-03-18 10:19:51 +09001108 missing_current_api := false
1109
Paul Duffin3a254982020-04-28 10:44:03 +01001110 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001111
Paul Duffin749f98f2019-12-30 17:23:46 +00001112 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001113 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001114 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001115 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001116 p := android.ExistentPathForSource(mctx, path)
1117 if !p.Valid() {
1118 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1119 missing_current_api = true
1120 }
1121 }
1122 }
1123
1124 if missing_current_api {
1125 script := "build/soong/scripts/gen-java-current-api-files.sh"
1126 p := android.ExistentPathForSource(mctx, script)
1127
1128 if !p.Valid() {
1129 panic(fmt.Sprintf("script file %s doesn't exist", script))
1130 }
1131
1132 mctx.ModuleErrorf("One or more current api files are missing. "+
1133 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001134 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001135 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001136 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001137 return
1138 }
1139
Paul Duffin3a254982020-04-28 10:44:03 +01001140 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001141 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001142 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001143
1144 // If the args needed to generate the stubs and API are the same then they
1145 // can be generated in a single invocation of metalava, otherwise they will
1146 // need separate invocations.
1147 if scope.createStubsSourceAndApiTogether {
1148 // Use the stubs source name for legacy reasons.
1149 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1150 } else {
1151 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1152
1153 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001154 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001155 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1156 }
1157
Paul Duffind1b3a922020-01-22 11:57:20 +00001158 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001159 }
1160
Paul Duffin43db9be2019-12-30 17:35:49 +00001161 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
1162 // for runtime
1163 module.createXmlFile(mctx)
1164
1165 // record java_sdk_library modules so that they are exported to make
1166 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1167 javaSdkLibrariesLock.Lock()
1168 defer javaSdkLibrariesLock.Unlock()
1169 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1170 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001171}
1172
1173func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001174 module.AddProperties(
1175 &module.sdkLibraryProperties,
Paul Duffinc5d954a2020-05-16 18:54:24 +01001176 &module.properties,
1177 &module.dexpreoptProperties,
1178 &module.deviceProperties,
1179 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001180 )
1181
Paul Duffinc5d954a2020-05-16 18:54:24 +01001182 module.properties.Installable = proptools.BoolPtr(true)
1183 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001184}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001185
Paul Duffin1a724e62020-05-08 13:44:43 +01001186// Defines how to name the individual component modules the sdk library creates.
1187type sdkLibraryComponentNamingScheme interface {
1188 stubsLibraryModuleName(scope *apiScope, baseName string) string
1189
1190 stubsSourceModuleName(scope *apiScope, baseName string) string
1191
1192 apiModuleName(scope *apiScope, baseName string) string
1193}
1194
1195type defaultNamingScheme struct {
1196}
1197
1198func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1199 return scope.stubsLibraryModuleName(baseName)
1200}
1201
1202func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1203 return scope.stubsSourceModuleName(baseName)
1204}
1205
1206func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1207 return scope.apiModuleName(baseName)
1208}
1209
1210var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1211
Paul Duffindef8a892020-05-08 15:36:30 +01001212type frameworkModulesNamingScheme struct {
1213}
1214
1215func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1216 suffix := scope.name
1217 if scope == apiScopeModuleLib {
1218 suffix = "module_libs_"
1219 }
1220 return suffix
1221}
1222
1223func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1224 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1225}
1226
1227func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1228 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1229}
1230
1231func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1232 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1233}
1234
1235var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1236
Anton Hansson0bd88d02020-05-25 12:20:51 +01001237func moduleStubLinkType(name string) (stub bool, ret linkType) {
1238 // This suffix-based approach is fragile and could potentially mis-trigger.
1239 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1240 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1241 return true, javaSdk
1242 }
1243 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1244 return true, javaSystem
1245 }
1246 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1247 return true, javaModule
1248 }
1249 if strings.HasSuffix(name, ".stubs.test") {
1250 return true, javaSystem
1251 }
1252 return false, javaPlatform
1253}
1254
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001255// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1256// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1257// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1258// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1259// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001260func SdkLibraryFactory() android.Module {
1261 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001262
1263 // Initialize information common between source and prebuilt.
1264 module.initCommon(&module.ModuleBase)
1265
Inseob Kimc0907f12019-02-08 21:00:45 +09001266 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001267 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001268 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001269
1270 // Initialize the map from scope to scope specific properties.
1271 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1272 for _, scope := range allApiScopes {
1273 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1274 }
1275 module.scopeToProperties = scopeToProperties
1276
Paul Duffin344c4ee2020-04-29 23:35:13 +01001277 // Add the properties containing visibility rules so that they are checked.
1278 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1279 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1280
Paul Duffin1a724e62020-05-08 13:44:43 +01001281 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
1282 if module.initCommonAfterDefaultsApplied(ctx) {
1283 module.CreateInternalModules(ctx)
1284 }
1285 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001286 return module
1287}
Colin Cross79c7c262019-04-17 11:11:46 -07001288
1289//
1290// SDK library prebuilts
1291//
1292
Paul Duffin56d44902020-01-31 13:36:25 +00001293// Properties associated with each api scope.
1294type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001295 Jars []string `android:"path"`
1296
1297 Sdk_version *string
1298
Colin Cross79c7c262019-04-17 11:11:46 -07001299 // List of shared java libs that this module has dependencies to
1300 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001301
Paul Duffin5fb82132020-04-29 20:45:27 +01001302 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001303 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001304
1305 // The current.txt
1306 Current_api string `android:"path"`
1307
1308 // The removed.txt
1309 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001310}
1311
Paul Duffin56d44902020-01-31 13:36:25 +00001312type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001313 // List of shared java libs, common to all scopes, that this module has
1314 // dependencies to
1315 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001316}
1317
Colin Cross79c7c262019-04-17 11:11:46 -07001318type sdkLibraryImport struct {
1319 android.ModuleBase
1320 android.DefaultableModuleBase
1321 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001322 android.ApexModuleBase
1323 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001324
1325 properties sdkLibraryImportProperties
1326
Paul Duffin6a2bd112020-04-07 19:27:04 +01001327 // Map from api scope to the scope specific property structure.
1328 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1329
Paul Duffin56d44902020-01-31 13:36:25 +00001330 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001331}
1332
1333var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1334
Paul Duffin6a2bd112020-04-07 19:27:04 +01001335// The type of a structure that contains a field of type sdkLibraryScopeProperties
1336// for each apiscope in allApiScopes, e.g. something like:
1337// struct {
1338// Public sdkLibraryScopeProperties
1339// System sdkLibraryScopeProperties
1340// ...
1341// }
1342var allScopeStructType = createAllScopePropertiesStructType()
1343
1344// Dynamically create a structure type for each apiscope in allApiScopes.
1345func createAllScopePropertiesStructType() reflect.Type {
1346 var fields []reflect.StructField
1347 for _, apiScope := range allApiScopes {
1348 field := reflect.StructField{
1349 Name: apiScope.fieldName,
1350 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1351 }
1352 fields = append(fields, field)
1353 }
1354
1355 return reflect.StructOf(fields)
1356}
1357
1358// Create an instance of the scope specific structure type and return a map
1359// from apiscope to a pointer to each scope specific field.
1360func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1361 allScopePropertiesPtr := reflect.New(allScopeStructType)
1362 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1363 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1364
1365 for _, apiScope := range allApiScopes {
1366 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1367 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1368 }
1369
1370 return allScopePropertiesPtr.Interface(), scopeProperties
1371}
1372
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001373// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001374func sdkLibraryImportFactory() android.Module {
1375 module := &sdkLibraryImport{}
1376
Paul Duffin6a2bd112020-04-07 19:27:04 +01001377 allScopeProperties, scopeToProperties := createPropertiesInstance()
1378 module.scopeProperties = scopeToProperties
1379 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001380
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001381 // Initialize information common between source and prebuilt.
1382 module.initCommon(&module.ModuleBase)
1383
Paul Duffin0bdcb272020-02-06 15:24:57 +00001384 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001385 android.InitApexModule(module)
1386 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001387 InitJavaModule(module, android.HostAndDeviceSupported)
1388
Paul Duffin1a724e62020-05-08 13:44:43 +01001389 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1390 if module.initCommonAfterDefaultsApplied(mctx) {
1391 module.createInternalModules(mctx)
1392 }
1393 })
Colin Cross79c7c262019-04-17 11:11:46 -07001394 return module
1395}
1396
1397func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1398 return &module.prebuilt
1399}
1400
1401func (module *sdkLibraryImport) Name() string {
1402 return module.prebuilt.Name(module.ModuleBase.Name())
1403}
1404
Paul Duffinbf735aa2020-05-08 15:01:19 +01001405func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001406
Paul Duffin50061512020-01-21 16:31:05 +00001407 // If the build is configured to use prebuilts then force this to be preferred.
1408 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1409 module.prebuilt.ForcePrefer()
1410 }
1411
Paul Duffin6a2bd112020-04-07 19:27:04 +01001412 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001413 if len(scopeProperties.Jars) == 0 {
1414 continue
1415 }
1416
Paul Duffinf6155722020-04-09 00:07:11 +01001417 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001418
1419 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +00001420 }
Colin Cross79c7c262019-04-17 11:11:46 -07001421
1422 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1423 javaSdkLibrariesLock.Lock()
1424 defer javaSdkLibrariesLock.Unlock()
1425 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1426}
1427
Paul Duffinbf735aa2020-05-08 15:01:19 +01001428func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001429 // Creates a java import for the jar with ".stubs" suffix
1430 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001431 Name *string
1432 Sdk_version *string
1433 Libs []string
1434 Jars []string
1435 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001436 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001437 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001438 props.Sdk_version = scopeProperties.Sdk_version
1439 // Prepend any of the libs from the legacy public properties to the libs for each of the
1440 // scopes to avoid having to duplicate them in each scope.
1441 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1442 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001443
Paul Duffindd89a282020-05-13 16:08:09 +01001444 // The imports are preferred if the java_sdk_library_import is preferred.
1445 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf6155722020-04-09 00:07:11 +01001446 mctx.CreateModule(ImportFactory, &props)
1447}
1448
Paul Duffinbf735aa2020-05-08 15:01:19 +01001449func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001450 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001451 Name *string
1452 Srcs []string
1453 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001454 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001455 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001456 props.Srcs = scopeProperties.Stub_srcs
1457 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001458
1459 // The stubs source is preferred if the java_sdk_library_import is preferred.
1460 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001461}
1462
Colin Cross79c7c262019-04-17 11:11:46 -07001463func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001464 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001465 if len(scopeProperties.Jars) == 0 {
1466 continue
1467 }
1468
1469 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001470 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin56d44902020-01-31 13:36:25 +00001471 }
Colin Cross79c7c262019-04-17 11:11:46 -07001472}
1473
1474func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1475 // Record the paths to the prebuilt stubs library.
1476 ctx.VisitDirectDeps(func(to android.Module) {
1477 tag := ctx.OtherModuleDependencyTag(to)
1478
Paul Duffin56d44902020-01-31 13:36:25 +00001479 if lib, ok := to.(Dependency); ok {
1480 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1481 apiScope := scopeTag.apiScope
1482 scopePaths := module.getScopePaths(apiScope)
1483 scopePaths.stubsHeaderPath = lib.HeaderJars()
1484 }
Colin Cross79c7c262019-04-17 11:11:46 -07001485 }
1486 })
1487}
1488
Paul Duffin56d44902020-01-31 13:36:25 +00001489func (module *sdkLibraryImport) sdkJars(
1490 ctx android.BaseModuleContext,
1491 sdkVersion sdkSpec) android.Paths {
1492
Paul Duffin50061512020-01-21 16:31:05 +00001493 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1494 if sdkVersion.version.isNumbered() {
1495 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1496 }
1497
Paul Duffin56d44902020-01-31 13:36:25 +00001498 var apiScope *apiScope
1499 switch sdkVersion.kind {
1500 case sdkSystem:
1501 apiScope = apiScopeSystem
1502 case sdkTest:
1503 apiScope = apiScopeTest
1504 default:
1505 apiScope = apiScopePublic
1506 }
1507
1508 paths := module.getScopePaths(apiScope)
1509 return paths.stubsHeaderPath
1510}
1511
Colin Cross79c7c262019-04-17 11:11:46 -07001512// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001513func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001514 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001515 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001516}
1517
1518// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001519func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001520 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001521 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001522}
Jiyong Parke3833882020-02-17 17:28:10 +09001523
1524//
1525// java_sdk_library_xml
1526//
1527type sdkLibraryXml struct {
1528 android.ModuleBase
1529 android.DefaultableModuleBase
1530 android.ApexModuleBase
1531
1532 properties sdkLibraryXmlProperties
1533
1534 outputFilePath android.OutputPath
1535 installDirPath android.InstallPath
1536}
1537
1538type sdkLibraryXmlProperties struct {
1539 // canonical name of the lib
1540 Lib_name *string
1541}
1542
1543// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1544// Not to be used directly by users. java_sdk_library internally uses this.
1545func sdkLibraryXmlFactory() android.Module {
1546 module := &sdkLibraryXml{}
1547
1548 module.AddProperties(&module.properties)
1549
1550 android.InitApexModule(module)
1551 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1552
1553 return module
1554}
1555
1556// from android.PrebuiltEtcModule
1557func (module *sdkLibraryXml) SubDir() string {
1558 return "permissions"
1559}
1560
1561// from android.PrebuiltEtcModule
1562func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1563 return module.outputFilePath
1564}
1565
1566// from android.ApexModule
1567func (module *sdkLibraryXml) AvailableFor(what string) bool {
1568 return true
1569}
1570
1571func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1572 // do nothing
1573}
1574
1575// File path to the runtime implementation library
1576func (module *sdkLibraryXml) implPath() string {
1577 implName := proptools.String(module.properties.Lib_name)
1578 if apexName := module.ApexName(); apexName != "" {
1579 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1580 // In most cases, this works fine. But when apex_name is set or override_apex is used
1581 // this can be wrong.
1582 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1583 }
1584 partition := "system"
1585 if module.SocSpecific() {
1586 partition = "vendor"
1587 } else if module.DeviceSpecific() {
1588 partition = "odm"
1589 } else if module.ProductSpecific() {
1590 partition = "product"
1591 } else if module.SystemExtSpecific() {
1592 partition = "system_ext"
1593 }
1594 return "/" + partition + "/framework/" + implName + ".jar"
1595}
1596
1597func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1598 libName := proptools.String(module.properties.Lib_name)
1599 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1600
1601 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1602 rule := android.NewRuleBuilder()
1603 rule.Command().
1604 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1605 Output(module.outputFilePath)
1606
1607 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1608
1609 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1610}
1611
1612func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1613 if !module.IsForPlatform() {
1614 return []android.AndroidMkEntries{android.AndroidMkEntries{
1615 Disabled: true,
1616 }}
1617 }
1618
1619 return []android.AndroidMkEntries{android.AndroidMkEntries{
1620 Class: "ETC",
1621 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1622 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1623 func(entries *android.AndroidMkEntries) {
1624 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1625 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1626 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1627 },
1628 },
1629 }}
1630}
Paul Duffin61871622020-02-10 13:37:10 +00001631
1632type sdkLibrarySdkMemberType struct {
1633 android.SdkMemberTypeBase
1634}
1635
1636func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1637 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1638}
1639
1640func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1641 _, ok := module.(*SdkLibrary)
1642 return ok
1643}
1644
1645func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1646 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1647}
1648
1649func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1650 return &sdkLibrarySdkMemberProperties{}
1651}
1652
1653type sdkLibrarySdkMemberProperties struct {
1654 android.SdkMemberPropertiesBase
1655
1656 // Scope to per scope properties.
1657 Scopes map[*apiScope]scopeProperties
1658
1659 // Additional libraries that the exported stubs libraries depend upon.
1660 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001661
1662 // The Java stubs source files.
1663 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01001664
1665 // The naming scheme.
1666 Naming_scheme *string
Paul Duffin61871622020-02-10 13:37:10 +00001667}
1668
1669type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01001670 Jars android.Paths
1671 StubsSrcJar android.Path
1672 CurrentApiFile android.Path
1673 RemovedApiFile android.Path
1674 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00001675}
1676
1677func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1678 sdk := variant.(*SdkLibrary)
1679
1680 s.Scopes = make(map[*apiScope]scopeProperties)
1681 for _, apiScope := range allApiScopes {
1682 paths := sdk.getScopePaths(apiScope)
1683 jars := paths.stubsImplPath
1684 if len(jars) > 0 {
1685 properties := scopeProperties{}
1686 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01001687 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffinf488ef22020-04-09 00:10:17 +01001688 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin75dcc802020-04-09 01:08:11 +01001689 properties.CurrentApiFile = paths.currentApiFilePath
1690 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffin61871622020-02-10 13:37:10 +00001691 s.Scopes[apiScope] = properties
1692 }
1693 }
1694
1695 s.Libs = sdk.properties.Libs
Paul Duffinf8e08b22020-05-13 16:54:55 +01001696 s.Naming_scheme = sdk.commonProperties.Naming_scheme
Paul Duffin61871622020-02-10 13:37:10 +00001697}
1698
1699func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01001700 if s.Naming_scheme != nil {
1701 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
1702 }
1703
Paul Duffin61871622020-02-10 13:37:10 +00001704 for _, apiScope := range allApiScopes {
1705 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01001706 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00001707
Paul Duffinf488ef22020-04-09 00:10:17 +01001708 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1709
Paul Duffin61871622020-02-10 13:37:10 +00001710 var jars []string
1711 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01001712 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00001713 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1714 jars = append(jars, dest)
1715 }
1716 scopeSet.AddProperty("jars", jars)
1717
Paul Duffinf488ef22020-04-09 00:10:17 +01001718 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1719 // the source files are also unpacked.
1720 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1721 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1722 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1723
Paul Duffin75dcc802020-04-09 01:08:11 +01001724 if properties.CurrentApiFile != nil {
1725 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1726 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1727 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1728 }
1729
1730 if properties.RemovedApiFile != nil {
1731 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1732 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1733 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1734 }
1735
Paul Duffin61871622020-02-10 13:37:10 +00001736 if properties.SdkVersion != "" {
1737 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1738 }
1739 }
1740 }
1741
1742 if len(s.Libs) > 0 {
1743 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1744 }
1745}