blob: dae045a05ecbb3b6067d9c514ee0b393709285ad [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
635// Name of the java_library module that compiles the stubs source.
636func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100637 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100638}
639
640// Name of the droidstubs module that generates the stubs source and may also
641// generate/check the API.
642func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100643 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100644}
645
646// Name of the droidstubs module that generates/checks the API. Only used if it
647// requires different arts to the stubs source generating module.
648func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100649 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100650}
651
Paul Duffin46fdda82020-05-14 15:39:10 +0100652// The component names for different outputs of the java_sdk_library.
653//
654// They are similar to the names used for the child modules it creates
655const (
656 stubsSourceComponentName = "stubs.source"
657
658 apiTxtComponentName = "api.txt"
659
660 removedApiTxtComponentName = "removed-api.txt"
661)
662
663// A regular expression to match tags that reference a specific stubs component.
664//
665// It will only match if given a valid scope and a valid component. It is verfy strict
666// to ensure it does not accidentally match a similar looking tag that should be processed
667// by the embedded Library.
668var tagSplitter = func() *regexp.Regexp {
669 // Given a list of literal string items returns a regular expression that will
670 // match any one of the items.
671 choice := func(items ...string) string {
672 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
673 }
674
675 // Regular expression to match one of the scopes.
676 scopesRegexp := choice(allScopeNames...)
677
678 // Regular expression to match one of the components.
679 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
680
681 // Regular expression to match any combination of one scope and one component.
682 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
683}()
684
685// For OutputFileProducer interface
686//
687// .<scope>.stubs.source
688// .<scope>.api.txt
689// .<scope>.removed-api.txt
690func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
691 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
692 scopeName := groups[1]
693 component := groups[2]
694
695 if scope, ok := scopeByName[scopeName]; ok {
696 paths := c.findScopePaths(scope)
697 if paths == nil {
698 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
699 }
700
701 switch component {
702 case stubsSourceComponentName:
703 if paths.stubsSrcJar.Valid() {
704 return android.Paths{paths.stubsSrcJar.Path()}, nil
705 }
706
707 case apiTxtComponentName:
708 if paths.currentApiFilePath.Valid() {
709 return android.Paths{paths.currentApiFilePath.Path()}, nil
710 }
711
712 case removedApiTxtComponentName:
713 if paths.removedApiFilePath.Valid() {
714 return android.Paths{paths.removedApiFilePath.Path()}, nil
715 }
716 }
717
718 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
719 } else {
720 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
721 }
722
723 } else {
724 return nil, nil
725 }
726}
727
Paul Duffin5ae30792020-05-20 11:52:25 +0100728func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000729 if c.scopePaths == nil {
730 c.scopePaths = make(map[*apiScope]*scopePaths)
731 }
732 paths := c.scopePaths[scope]
733 if paths == nil {
734 paths = &scopePaths{}
735 c.scopePaths[scope] = paths
736 }
737
738 return paths
739}
740
Paul Duffin5ae30792020-05-20 11:52:25 +0100741func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
742 if c.scopePaths == nil {
743 return nil
744 }
745
746 return c.scopePaths[scope]
747}
748
749// If this does not support the requested api scope then find the closest available
750// scope it does support. Returns nil if no such scope is available.
751func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
752 for s := scope; s != nil; s = s.extends {
753 if paths := c.findScopePaths(s); paths != nil {
754 return paths
755 }
756 }
757
758 // This should never happen outside tests as public should be the base scope for every
759 // scope and is enabled by default.
760 return nil
761}
762
Paul Duffina3fb67d2020-05-20 14:20:02 +0100763func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-05-20 12:19:10 +0100764
765 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
766 if sdkVersion.version.isNumbered() {
767 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
768 }
769
770 var apiScope *apiScope
771 switch sdkVersion.kind {
772 case sdkSystem:
773 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100774 case sdkModule:
775 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100776 case sdkTest:
777 apiScope = apiScopeTest
Paul Duffin5a757b12020-06-02 13:00:08 +0100778 case sdkSystemServer:
779 apiScope = apiScopeSystemServer
Paul Duffin47624362020-05-20 12:19:10 +0100780 default:
781 apiScope = apiScopePublic
782 }
783
Paul Duffin5ae30792020-05-20 11:52:25 +0100784 paths := c.findClosestScopePath(apiScope)
785 if paths == nil {
786 var scopes []string
787 for _, s := range allApiScopes {
788 if c.findScopePaths(s) != nil {
789 scopes = append(scopes, s.name)
790 }
791 }
792 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
793 return nil
794 }
795
Paul Duffina3fb67d2020-05-20 14:20:02 +0100796 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100797}
798
Paul Duffin64e61992020-05-15 10:20:31 +0100799func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
800 componentProps := &struct {
801 SdkLibraryToImplicitlyTrack *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100802 }{}
803
804 if c.sharedLibrary() {
Paul Duffin64e61992020-05-15 10:20:31 +0100805 // Mark the stubs library as being components of this java_sdk_library so that
806 // any app that includes code which depends (directly or indirectly) on the stubs
807 // library will have the appropriate <uses-library> invocation inserted into its
808 // manifest if necessary.
Paul Duffind11e78e2020-05-15 20:37:11 +0100809 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin64e61992020-05-15 10:20:31 +0100810 }
811
812 return componentProps
813}
814
Paul Duffind11e78e2020-05-15 20:37:11 +0100815// Check if this can be used as a shared library.
816func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
817 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
818}
819
Paul Duffin64e61992020-05-15 10:20:31 +0100820// Properties related to the use of a module as an component of a java_sdk_library.
821type SdkLibraryComponentProperties struct {
822
823 // The name of the java_sdk_library/_import to add to a <uses-library> entry
824 // in the AndroidManifest.xml of any Android app that includes code that references
825 // this module. If not set then no java_sdk_library/_import is tracked.
826 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
827}
828
829// Structure to be embedded in a module struct that needs to support the
830// SdkLibraryComponentDependency interface.
831type EmbeddableSdkLibraryComponent struct {
832 sdkLibraryComponentProperties SdkLibraryComponentProperties
833}
834
835func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
836 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
837}
838
839// to satisfy SdkLibraryComponentDependency
840func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
841 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
842 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
843 }
844 return nil
845}
846
847// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
848// (including the java_sdk_library) itself.
849type SdkLibraryComponentDependency interface {
850 // The optional name of the sdk library that should be implicitly added to the
851 // AndroidManifest of an app that contains code which references the sdk library.
852 //
853 // Returns an array containing 0 or 1 items rather than a *string to make it easier
854 // to append this to the list of exported sdk libraries.
855 OptionalImplicitSdkLibrary() []string
856}
857
858// Make sure that all the module types that are components of java_sdk_library/_import
859// and which can be referenced (directly or indirectly) from an android app implement
860// the SdkLibraryComponentDependency interface.
861var _ SdkLibraryComponentDependency = (*Library)(nil)
862var _ SdkLibraryComponentDependency = (*Import)(nil)
863var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
864var _ SdkLibraryComponentDependency = (*sdkLibraryImport)(nil)
865
866// Provides access to sdk_version related header and implentation jars.
867type SdkLibraryDependency interface {
868 SdkLibraryComponentDependency
869
870 // Get the header jars appropriate for the supplied sdk_version.
871 //
872 // These are turbine generated jars so they only change if the externals of the
873 // class changes but it does not contain and implementation or JavaDoc.
874 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
875
876 // Get the implementation jars appropriate for the supplied sdk version.
877 //
878 // These are either the implementation jar for the whole sdk library or the implementation
879 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
880 // they are identical to the corresponding header jars.
881 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
882}
883
Inseob Kimc0907f12019-02-08 21:00:45 +0900884type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900885 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900886
Sundong Ahn054b19a2018-10-19 13:46:09 +0900887 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900888
Paul Duffin3a254982020-04-28 10:44:03 +0100889 // Map from api scope to the scope specific property structure.
890 scopeToProperties map[*apiScope]*ApiScopeProperties
891
Paul Duffin56d44902020-01-31 13:36:25 +0000892 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900893}
894
Inseob Kimc0907f12019-02-08 21:00:45 +0900895var _ Dependency = (*SdkLibrary)(nil)
896var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800897
Paul Duffin3a254982020-04-28 10:44:03 +0100898func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
899 return module.sdkLibraryProperties.Generate_system_and_test_apis
900}
901
902func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
903 // Check to see if any scopes have been explicitly enabled. If any have then all
904 // must be.
905 anyScopesExplicitlyEnabled := false
906 for _, scope := range allApiScopes {
907 scopeProperties := module.scopeToProperties[scope]
908 if scopeProperties.Enabled != nil {
909 anyScopesExplicitlyEnabled = true
910 break
911 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000912 }
Paul Duffin3a254982020-04-28 10:44:03 +0100913
914 var generatedScopes apiScopes
915 enabledScopes := make(map[*apiScope]struct{})
916 for _, scope := range allApiScopes {
917 scopeProperties := module.scopeToProperties[scope]
918 // If any scopes are explicitly enabled then ignore the legacy enabled status.
919 // This is to ensure that any new usages of this module type do not rely on legacy
920 // behaviour.
921 defaultEnabledStatus := false
922 if anyScopesExplicitlyEnabled {
923 defaultEnabledStatus = scope.defaultEnabledStatus
924 } else {
925 defaultEnabledStatus = scope.legacyEnabledStatus(module)
926 }
927 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
928 if enabled {
929 enabledScopes[scope] = struct{}{}
930 generatedScopes = append(generatedScopes, scope)
931 }
932 }
933
934 // Now check to make sure that any scope that is extended by an enabled scope is also
935 // enabled.
936 for _, scope := range allApiScopes {
937 if _, ok := enabledScopes[scope]; ok {
938 extends := scope.extends
939 if extends != nil {
940 if _, ok := enabledScopes[extends]; !ok {
941 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
942 }
943 }
944 }
945 }
946
947 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000948}
949
Paul Duffine74ac732020-02-06 13:51:46 +0000950var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
951
Jiyong Parke3833882020-02-17 17:28:10 +0900952func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
953 if dt, ok := depTag.(dependencyTag); ok {
954 return dt == xmlPermissionsFileTag
955 }
956 return false
957}
958
Paul Duffin9d582cc2020-05-16 15:52:12 +0100959var implLibraryTag = dependencyTag{name: "impl-library"}
960
Inseob Kimc0907f12019-02-08 21:00:45 +0900961func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100962 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000963 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100964 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000965
Paul Duffina377e4c2020-04-29 13:30:54 +0100966 // If the stubs source and API cannot be generated together then add an additional dependency on
967 // the API module.
968 if apiScope.createStubsSourceAndApiTogether {
969 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100970 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100971 } else {
972 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100973 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
974 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100975 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900976 }
977
Paul Duffind11e78e2020-05-15 20:37:11 +0100978 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100979 // Add dependency to the rule for generating the implementation library.
980 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
981
Paul Duffind11e78e2020-05-15 20:37:11 +0100982 if module.sharedLibrary() {
983 // Add dependency to the rule for generating the xml permissions file
984 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
985 }
Paul Duffine74ac732020-02-06 13:51:46 +0000986
Paul Duffind11e78e2020-05-15 20:37:11 +0100987 // Only add the deps for the library if it is actually going to be built.
988 module.Library.deps(ctx)
989 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900990}
991
Paul Duffin46fdda82020-05-14 15:39:10 +0100992func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
993 paths, err := module.commonOutputFiles(tag)
994 if paths == nil && err == nil {
995 return module.Library.OutputFiles(tag)
996 } else {
997 return paths, err
998 }
999}
1000
Inseob Kimc0907f12019-02-08 21:00:45 +09001001func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001002 // Only build an implementation library if required.
1003 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001004 module.Library.GenerateAndroidBuildActions(ctx)
1005 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001006
Sundong Ahn57368eb2018-07-06 11:20:23 +09001007 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001008 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001009 // the recorded paths will be returned depending on the link type of the caller.
1010 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001011 tag := ctx.OtherModuleDependencyTag(to)
1012
Paul Duffin5fb82132020-04-29 20:45:27 +01001013 // Extract information from any of the scope specific dependencies.
1014 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1015 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +01001016 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +01001017
1018 // Extract information from the dependency. The exact information extracted
1019 // is determined by the nature of the dependency which is determined by the tag.
1020 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001021 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001022 })
1023}
1024
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001025func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffind11e78e2020-05-15 20:37:11 +01001026 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001027 return nil
1028 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001029 entriesList := module.Library.AndroidMkEntries()
1030 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -07001031 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001032 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001033}
1034
Jiyong Parkc678ad32018-04-10 13:07:10 +09001035// Module name of the runtime implementation library
Paul Duffin9d582cc2020-05-16 15:52:12 +01001036func (module *SdkLibrary) implLibraryModuleName() string {
1037 return module.BaseModuleName() + ".impl"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001038}
1039
Jiyong Parkc678ad32018-04-10 13:07:10 +09001040// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +09001041func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001042 return module.BaseModuleName() + sdkXmlFileSuffix
1043}
1044
Anton Hansson6bb88102020-03-27 19:43:19 +00001045// The dist path of the stub artifacts
1046func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1047 if module.ModuleBase.Owner() != "" {
1048 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1049 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1050 return path.Join("apistubs", "core", apiScope.name)
1051 } else {
1052 return path.Join("apistubs", "android", apiScope.name)
1053 }
1054}
1055
Paul Duffin12ceb462019-12-24 20:31:31 +00001056// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +01001057func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +01001058 scopeProperties := module.scopeToProperties[apiScope]
1059 if scopeProperties.Sdk_version != nil {
1060 return proptools.String(scopeProperties.Sdk_version)
1061 }
1062
Paul Duffin12ceb462019-12-24 20:31:31 +00001063 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1064 if sdkDep.hasStandardLibs() {
1065 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001066 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001067 } else {
1068 // Otherwise, use no system module.
1069 return "none"
1070 }
1071}
1072
Paul Duffind1b3a922020-01-22 11:57:20 +00001073func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1074 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001075}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001076
Paul Duffind1b3a922020-01-22 11:57:20 +00001077func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1078 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001079}
1080
Paul Duffin9d582cc2020-05-16 15:52:12 +01001081// Creates the implementation java library
1082func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
1083 props := struct {
1084 Name *string
1085 Visibility []string
1086 }{
1087 Name: proptools.StringPtr(module.implLibraryModuleName()),
1088 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
1089 }
1090
1091 properties := []interface{}{
1092 &module.properties,
1093 &module.protoProperties,
1094 &module.deviceProperties,
1095 &module.dexpreoptProperties,
1096 &props,
1097 module.sdkComponentPropertiesForChildLibrary(),
1098 }
1099 mctx.CreateModule(LibraryFactory, properties...)
1100}
1101
Jiyong Parkc678ad32018-04-10 13:07:10 +09001102// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001103func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001104 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001105 Name *string
1106 Visibility []string
1107 Srcs []string
1108 Installable *bool
1109 Sdk_version *string
1110 System_modules *string
1111 Patch_module *string
1112 Libs []string
1113 Compile_dex *bool
1114 Java_version *string
1115 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001116 Pdk struct {
1117 Enabled *bool
1118 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001119 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001120 Openjdk9 struct {
1121 Srcs []string
1122 Javacflags []string
1123 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001124 Dist struct {
1125 Targets []string
1126 Dest *string
1127 Dir *string
1128 Tag *string
1129 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001130 }{}
1131
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001132 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +01001133
1134 // If stubs_library_visibility is not set then the created module will use the
1135 // visibility of this module.
1136 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1137 props.Visibility = visibility
1138
Jiyong Parkc678ad32018-04-10 13:07:10 +09001139 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001140 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001141 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001142 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001143 props.System_modules = module.deviceProperties.System_modules
1144 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001145 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001146 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +01001147 // The stub-annotations library contains special versions of the annotations
1148 // with CLASS retention policy, so that they're kept.
1149 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1150 props.Libs = append(props.Libs, "stub-annotations")
1151 }
Jiyong Park82484c02018-04-23 21:41:26 +09001152 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001153 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1154 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hanssoncf4dd4c2020-05-21 09:21:57 +01001155 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1156 // interop with older developer tools that don't support 1.9.
1157 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinc5d954a2020-05-16 18:54:24 +01001158 if module.deviceProperties.Compile_dex != nil {
1159 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001160 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001161
Anton Hansson6bb88102020-03-27 19:43:19 +00001162 // Dist the class jar artifact for sdk builds.
1163 if !Bool(module.sdkLibraryProperties.No_dist) {
1164 props.Dist.Targets = []string{"sdk", "win_sdk"}
1165 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1166 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1167 props.Dist.Tag = proptools.StringPtr(".jar")
1168 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001169
Paul Duffin64e61992020-05-15 10:20:31 +01001170 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001171}
1172
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001173// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001174// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +01001175func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001176 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001177 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001178 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001179 Srcs []string
1180 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001181 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001182 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001183 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001184 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001185 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001186 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +01001187 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001188 Merge_annotations_dirs []string
1189 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001190 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001191 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001192 Current ApiToCheck
1193 Last_released ApiToCheck
1194 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001195
1196 Api_lint struct {
1197 Enabled *bool
1198 New_since *string
1199 Baseline_file *string
1200 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001201 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001202 Aidl struct {
1203 Include_dirs []string
1204 Local_include_dirs []string
1205 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001206 Dist struct {
1207 Targets []string
1208 Dest *string
1209 Dir *string
1210 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001211 }{}
1212
Paul Duffinda364252020-04-28 14:08:32 +01001213 // The stubs source processing uses the same compile time classpath when extracting the
1214 // API from the implementation library as it does when compiling it. i.e. the same
1215 // * sdk version
1216 // * system_modules
1217 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001218
Paul Duffina377e4c2020-04-29 13:30:54 +01001219 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001220
1221 // If stubs_source_visibility is not set then the created module will use the
1222 // visibility of this module.
1223 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1224 props.Visibility = visibility
1225
Paul Duffinc5d954a2020-05-16 18:54:24 +01001226 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1227 props.Sdk_version = module.deviceProperties.Sdk_version
1228 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001229 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001230 // A droiddoc module has only one Libs property and doesn't distinguish between
1231 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001232 props.Libs = module.properties.Libs
1233 props.Libs = append(props.Libs, module.properties.Static_libs...)
1234 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1235 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1236 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001237
Paul Duffin2ce1e812020-05-20 19:35:27 +01001238 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001239 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1240 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1241
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001242 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001243 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001244 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001245 }
1246 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001247 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001248 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1249 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001250 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001251 disabledWarnings := []string{
1252 "MissingPermission",
1253 "BroadcastBehavior",
1254 "HiddenSuperclass",
1255 "DeprecationMismatch",
1256 "UnavailableSymbol",
1257 "SdkConstant",
1258 "HiddenTypeParameter",
1259 "Todo",
1260 "Typo",
1261 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001262 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001263
Paul Duffina377e4c2020-04-29 13:30:54 +01001264 if !createStubSources {
1265 // Stubs are not required.
1266 props.Generate_stubs = proptools.BoolPtr(false)
1267 }
1268
Paul Duffin3c7c3472020-04-07 18:50:10 +01001269 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001270 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001271 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001272 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001273
Paul Duffina377e4c2020-04-29 13:30:54 +01001274 if createApi {
1275 // List of APIs identified from the provided source files are created. They are later
1276 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1277 // last-released (a.k.a numbered) list of API.
1278 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1279 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1280 apiDir := module.getApiDir()
1281 currentApiFileName = path.Join(apiDir, currentApiFileName)
1282 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001283
Paul Duffina377e4c2020-04-29 13:30:54 +01001284 // check against the not-yet-release API
1285 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1286 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001287
Paul Duffina377e4c2020-04-29 13:30:54 +01001288 if !apiScope.unstable {
1289 // check against the latest released API
1290 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1291 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1292 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1293 module.latestRemovedApiFilegroupName(apiScope))
1294 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001295
Paul Duffina377e4c2020-04-29 13:30:54 +01001296 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1297 // Enable api lint.
1298 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1299 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001300
Paul Duffina377e4c2020-04-29 13:30:54 +01001301 // If it exists then pass a lint-baseline.txt through to droidstubs.
1302 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1303 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1304 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1305 if err != nil {
1306 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1307 }
1308 if len(paths) == 1 {
1309 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1310 } else if len(paths) != 0 {
1311 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1312 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001313 }
1314 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001315
Paul Duffina377e4c2020-04-29 13:30:54 +01001316 // Dist the api txt artifact for sdk builds.
1317 if !Bool(module.sdkLibraryProperties.No_dist) {
1318 props.Dist.Targets = []string{"sdk", "win_sdk"}
1319 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1320 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1321 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001322 }
1323
Colin Cross84dfc3d2019-09-25 11:33:01 -07001324 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001325}
1326
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001327func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1328 depTag := mctx.OtherModuleDependencyTag(dep)
1329 if depTag == xmlPermissionsFileTag {
1330 return true
1331 }
1332 return module.Library.DepIsInSameApex(mctx, dep)
1333}
1334
Jiyong Parkc678ad32018-04-10 13:07:10 +09001335// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001336func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001337 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001338 Name *string
1339 Lib_name *string
1340 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001341 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001342 Name: proptools.StringPtr(module.xmlFileName()),
1343 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1344 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001345 }
Jiyong Parke3833882020-02-17 17:28:10 +09001346
Jiyong Parke3833882020-02-17 17:28:10 +09001347 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001348}
1349
Paul Duffin50061512020-01-21 16:31:05 +00001350func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001351 var ver sdkVersion
1352 var kind sdkKind
1353 if s.usePrebuilt(ctx) {
1354 ver = s.version
1355 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001356 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001357 // We don't have prebuilt SDK for the specific sdkVersion.
1358 // Instead of breaking the build, fallback to use "system_current"
1359 ver = sdkVersionCurrent
1360 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001361 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001362
1363 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001364 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001365 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001366 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001367 if ctx.Config().AllowMissingDependencies() {
1368 return android.Paths{android.PathForSource(ctx, jar)}
1369 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001370 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001371 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001372 return nil
1373 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001374 return android.Paths{jarPath.Path()}
1375}
1376
Paul Duffinbf19a972020-05-26 13:21:35 +01001377// Get the apex name for module, "" if it is for platform.
1378func getApexNameForModule(module android.Module) string {
1379 if apex, ok := module.(android.ApexModule); ok {
1380 return apex.ApexName()
1381 }
1382
1383 return ""
1384}
1385
1386// Check to see if the other module is within the same named APEX as this module.
1387//
1388// If either this or the other module are on the platform then this will return
1389// false.
1390func (module *SdkLibrary) withinSameApexAs(other android.Module) bool {
1391 name := module.ApexName()
1392 return name != "" && getApexNameForModule(other) == name
1393}
1394
Paul Duffin47624362020-05-20 12:19:10 +01001395func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park27fc4142020-05-28 00:19:53 +09001396 // If the client doesn't set sdk_version, but if this library prefers stubs over
1397 // the impl library, let's provide the widest API surface possible. To do so,
1398 // force override sdk_version to module_current so that the closest possible API
1399 // surface could be found in selectHeaderJarsForSdkVersion
1400 if module.defaultsToStubs() && !sdkVersion.specified() {
1401 sdkVersion = sdkSpecFrom("module_current")
1402 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001403
Paul Duffin2e7ed652020-05-26 18:13:57 +01001404 // Only provide access to the implementation library if it is actually built.
1405 if module.requiresRuntimeImplementationLibrary() {
1406 // Check any special cases for java_sdk_library.
1407 //
1408 // Only allow access to the implementation library in the following condition:
1409 // * No sdk_version specified on the referencing module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001410 // * The referencing module is in the same apex as this.
1411 if sdkVersion.kind == sdkPrivate || module.withinSameApexAs(ctx.Module()) {
Paul Duffin2e7ed652020-05-26 18:13:57 +01001412 if headerJars {
1413 return module.HeaderJars()
1414 } else {
1415 return module.ImplementationJars()
1416 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001417 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001418 }
Paul Duffin47624362020-05-20 12:19:10 +01001419
Paul Duffina3fb67d2020-05-20 14:20:02 +01001420 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001421}
1422
Sundong Ahn241cd372018-07-13 16:16:44 +09001423// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001424func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1425 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1426}
1427
1428// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001429func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001430 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001431}
1432
Sundong Ahn80a87b32019-05-13 15:02:50 +09001433func (module *SdkLibrary) SetNoDist() {
1434 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1435}
1436
Colin Cross571cccf2019-02-04 11:22:08 -08001437var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1438
Jiyong Park82484c02018-04-23 21:41:26 +09001439func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001440 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001441 return &[]string{}
1442 }).(*[]string)
1443}
1444
Paul Duffin749f98f2019-12-30 17:23:46 +00001445func (module *SdkLibrary) getApiDir() string {
1446 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1447}
1448
Jiyong Parkc678ad32018-04-10 13:07:10 +09001449// For a java_sdk_library module, create internal modules for stubs, docs,
1450// runtime libs and xml file. If requested, the stubs and docs are created twice
1451// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001452func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1453 // If the module has been disabled then don't create any child modules.
1454 if !module.Enabled() {
1455 return
1456 }
1457
Paul Duffinc5d954a2020-05-16 18:54:24 +01001458 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001459 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001460 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001461 }
1462
Paul Duffin37e0b772019-12-30 17:20:10 +00001463 // If this builds against standard libraries (i.e. is not part of the core libraries)
1464 // then assume it provides both system and test apis. Otherwise, assume it does not and
1465 // also assume it does not contribute to the dist build.
1466 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1467 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001468 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001469 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1470
Inseob Kim8098faa2019-03-18 10:19:51 +09001471 missing_current_api := false
1472
Paul Duffin3a254982020-04-28 10:44:03 +01001473 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001474
Paul Duffin749f98f2019-12-30 17:23:46 +00001475 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001476 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001477 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001478 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001479 p := android.ExistentPathForSource(mctx, path)
1480 if !p.Valid() {
1481 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1482 missing_current_api = true
1483 }
1484 }
1485 }
1486
1487 if missing_current_api {
1488 script := "build/soong/scripts/gen-java-current-api-files.sh"
1489 p := android.ExistentPathForSource(mctx, script)
1490
1491 if !p.Valid() {
1492 panic(fmt.Sprintf("script file %s doesn't exist", script))
1493 }
1494
1495 mctx.ModuleErrorf("One or more current api files are missing. "+
1496 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001497 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001498 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001499 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001500 return
1501 }
1502
Paul Duffin3a254982020-04-28 10:44:03 +01001503 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001504 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001505 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001506
1507 // If the args needed to generate the stubs and API are the same then they
1508 // can be generated in a single invocation of metalava, otherwise they will
1509 // need separate invocations.
1510 if scope.createStubsSourceAndApiTogether {
1511 // Use the stubs source name for legacy reasons.
1512 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1513 } else {
1514 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1515
1516 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001517 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001518 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1519 }
1520
Paul Duffind1b3a922020-01-22 11:57:20 +00001521 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001522 }
1523
Paul Duffind11e78e2020-05-15 20:37:11 +01001524 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001525 // Create child module to create an implementation library.
1526 //
1527 // This temporarily creates a second implementation library that can be explicitly
1528 // referenced.
1529 //
1530 // TODO(b/156618935) - update comment once only one implementation library is created.
1531 module.createImplLibrary(mctx)
1532
Paul Duffind11e78e2020-05-15 20:37:11 +01001533 // Only create an XML permissions file that declares the library as being usable
1534 // as a shared library if required.
1535 if module.sharedLibrary() {
1536 module.createXmlFile(mctx)
1537 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001538
1539 // record java_sdk_library modules so that they are exported to make
1540 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1541 javaSdkLibrariesLock.Lock()
1542 defer javaSdkLibrariesLock.Unlock()
1543 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1544 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001545}
1546
1547func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001548 module.AddProperties(
1549 &module.sdkLibraryProperties,
Paul Duffinc5d954a2020-05-16 18:54:24 +01001550 &module.properties,
1551 &module.dexpreoptProperties,
1552 &module.deviceProperties,
1553 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001554 )
1555
Paul Duffin64e61992020-05-15 10:20:31 +01001556 module.initSdkLibraryComponent(&module.ModuleBase)
1557
Paul Duffinc5d954a2020-05-16 18:54:24 +01001558 module.properties.Installable = proptools.BoolPtr(true)
1559 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001560}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001561
Paul Duffind11e78e2020-05-15 20:37:11 +01001562func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1563 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1564}
1565
Jiyong Park27fc4142020-05-28 00:19:53 +09001566func (module *SdkLibrary) defaultsToStubs() bool {
1567 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1568}
1569
Paul Duffin1a724e62020-05-08 13:44:43 +01001570// Defines how to name the individual component modules the sdk library creates.
1571type sdkLibraryComponentNamingScheme interface {
1572 stubsLibraryModuleName(scope *apiScope, baseName string) string
1573
1574 stubsSourceModuleName(scope *apiScope, baseName string) string
1575
1576 apiModuleName(scope *apiScope, baseName string) string
1577}
1578
1579type defaultNamingScheme struct {
1580}
1581
1582func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1583 return scope.stubsLibraryModuleName(baseName)
1584}
1585
1586func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1587 return scope.stubsSourceModuleName(baseName)
1588}
1589
1590func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1591 return scope.apiModuleName(baseName)
1592}
1593
1594var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1595
Paul Duffindef8a892020-05-08 15:36:30 +01001596type frameworkModulesNamingScheme struct {
1597}
1598
1599func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1600 suffix := scope.name
1601 if scope == apiScopeModuleLib {
1602 suffix = "module_libs_"
1603 }
1604 return suffix
1605}
1606
1607func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1608 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1609}
1610
1611func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1612 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1613}
1614
1615func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1616 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1617}
1618
1619var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1620
Anton Hansson0bd88d02020-05-25 12:20:51 +01001621func moduleStubLinkType(name string) (stub bool, ret linkType) {
1622 // This suffix-based approach is fragile and could potentially mis-trigger.
1623 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1624 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1625 return true, javaSdk
1626 }
1627 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1628 return true, javaSystem
1629 }
1630 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1631 return true, javaModule
1632 }
1633 if strings.HasSuffix(name, ".stubs.test") {
1634 return true, javaSystem
1635 }
1636 return false, javaPlatform
1637}
1638
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001639// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1640// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1641// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1642// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1643// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001644func SdkLibraryFactory() android.Module {
1645 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001646
1647 // Initialize information common between source and prebuilt.
1648 module.initCommon(&module.ModuleBase)
1649
Inseob Kimc0907f12019-02-08 21:00:45 +09001650 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001651 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001652 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001653
1654 // Initialize the map from scope to scope specific properties.
1655 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1656 for _, scope := range allApiScopes {
1657 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1658 }
1659 module.scopeToProperties = scopeToProperties
1660
Paul Duffin344c4ee2020-04-29 23:35:13 +01001661 // Add the properties containing visibility rules so that they are checked.
Paul Duffin9d582cc2020-05-16 15:52:12 +01001662 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001663 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1664 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1665
Paul Duffin1a724e62020-05-08 13:44:43 +01001666 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001667 // If no implementation is required then it cannot be used as a shared library
1668 // either.
1669 if !module.requiresRuntimeImplementationLibrary() {
1670 // If shared_library has been explicitly set to true then it is incompatible
1671 // with api_only: true.
1672 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1673 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1674 }
1675 // Set shared_library: false.
1676 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1677 }
1678
Paul Duffin1a724e62020-05-08 13:44:43 +01001679 if module.initCommonAfterDefaultsApplied(ctx) {
1680 module.CreateInternalModules(ctx)
1681 }
1682 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001683 return module
1684}
Colin Cross79c7c262019-04-17 11:11:46 -07001685
1686//
1687// SDK library prebuilts
1688//
1689
Paul Duffin56d44902020-01-31 13:36:25 +00001690// Properties associated with each api scope.
1691type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001692 Jars []string `android:"path"`
1693
1694 Sdk_version *string
1695
Colin Cross79c7c262019-04-17 11:11:46 -07001696 // List of shared java libs that this module has dependencies to
1697 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001698
Paul Duffin5fb82132020-04-29 20:45:27 +01001699 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001700 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001701
1702 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001703 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001704
1705 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001706 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001707}
1708
Paul Duffin56d44902020-01-31 13:36:25 +00001709type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001710 // List of shared java libs, common to all scopes, that this module has
1711 // dependencies to
1712 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001713}
1714
Colin Cross79c7c262019-04-17 11:11:46 -07001715type sdkLibraryImport struct {
1716 android.ModuleBase
1717 android.DefaultableModuleBase
1718 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001719 android.ApexModuleBase
1720 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001721
1722 properties sdkLibraryImportProperties
1723
Paul Duffin6a2bd112020-04-07 19:27:04 +01001724 // Map from api scope to the scope specific property structure.
1725 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1726
Paul Duffin56d44902020-01-31 13:36:25 +00001727 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001728}
1729
1730var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1731
Paul Duffin6a2bd112020-04-07 19:27:04 +01001732// The type of a structure that contains a field of type sdkLibraryScopeProperties
1733// for each apiscope in allApiScopes, e.g. something like:
1734// struct {
1735// Public sdkLibraryScopeProperties
1736// System sdkLibraryScopeProperties
1737// ...
1738// }
1739var allScopeStructType = createAllScopePropertiesStructType()
1740
1741// Dynamically create a structure type for each apiscope in allApiScopes.
1742func createAllScopePropertiesStructType() reflect.Type {
1743 var fields []reflect.StructField
1744 for _, apiScope := range allApiScopes {
1745 field := reflect.StructField{
1746 Name: apiScope.fieldName,
1747 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1748 }
1749 fields = append(fields, field)
1750 }
1751
1752 return reflect.StructOf(fields)
1753}
1754
1755// Create an instance of the scope specific structure type and return a map
1756// from apiscope to a pointer to each scope specific field.
1757func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1758 allScopePropertiesPtr := reflect.New(allScopeStructType)
1759 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1760 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1761
1762 for _, apiScope := range allApiScopes {
1763 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1764 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1765 }
1766
1767 return allScopePropertiesPtr.Interface(), scopeProperties
1768}
1769
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001770// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001771func sdkLibraryImportFactory() android.Module {
1772 module := &sdkLibraryImport{}
1773
Paul Duffin6a2bd112020-04-07 19:27:04 +01001774 allScopeProperties, scopeToProperties := createPropertiesInstance()
1775 module.scopeProperties = scopeToProperties
1776 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001777
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001778 // Initialize information common between source and prebuilt.
1779 module.initCommon(&module.ModuleBase)
1780
Paul Duffin0bdcb272020-02-06 15:24:57 +00001781 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001782 android.InitApexModule(module)
1783 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001784 InitJavaModule(module, android.HostAndDeviceSupported)
1785
Paul Duffin1a724e62020-05-08 13:44:43 +01001786 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1787 if module.initCommonAfterDefaultsApplied(mctx) {
1788 module.createInternalModules(mctx)
1789 }
1790 })
Colin Cross79c7c262019-04-17 11:11:46 -07001791 return module
1792}
1793
1794func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1795 return &module.prebuilt
1796}
1797
1798func (module *sdkLibraryImport) Name() string {
1799 return module.prebuilt.Name(module.ModuleBase.Name())
1800}
1801
Paul Duffinbf735aa2020-05-08 15:01:19 +01001802func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001803
Paul Duffin50061512020-01-21 16:31:05 +00001804 // If the build is configured to use prebuilts then force this to be preferred.
1805 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1806 module.prebuilt.ForcePrefer()
1807 }
1808
Paul Duffin6a2bd112020-04-07 19:27:04 +01001809 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001810 if len(scopeProperties.Jars) == 0 {
1811 continue
1812 }
1813
Paul Duffinf6155722020-04-09 00:07:11 +01001814 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001815
Paul Duffin533f9c72020-05-20 16:18:00 +01001816 if len(scopeProperties.Stub_srcs) > 0 {
1817 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1818 }
Paul Duffin56d44902020-01-31 13:36:25 +00001819 }
Colin Cross79c7c262019-04-17 11:11:46 -07001820
1821 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1822 javaSdkLibrariesLock.Lock()
1823 defer javaSdkLibrariesLock.Unlock()
1824 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1825}
1826
Paul Duffinbf735aa2020-05-08 15:01:19 +01001827func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001828 // Creates a java import for the jar with ".stubs" suffix
1829 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001830 Name *string
1831 Sdk_version *string
1832 Libs []string
1833 Jars []string
1834 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001835 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001836 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001837 props.Sdk_version = scopeProperties.Sdk_version
1838 // Prepend any of the libs from the legacy public properties to the libs for each of the
1839 // scopes to avoid having to duplicate them in each scope.
1840 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1841 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001842
Paul Duffindd89a282020-05-13 16:08:09 +01001843 // The imports are preferred if the java_sdk_library_import is preferred.
1844 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001845
1846 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001847}
1848
Paul Duffinbf735aa2020-05-08 15:01:19 +01001849func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001850 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001851 Name *string
1852 Srcs []string
1853 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001854 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001855 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001856 props.Srcs = scopeProperties.Stub_srcs
1857 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001858
1859 // The stubs source is preferred if the java_sdk_library_import is preferred.
1860 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001861}
1862
Colin Cross79c7c262019-04-17 11:11:46 -07001863func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001864 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001865 if len(scopeProperties.Jars) == 0 {
1866 continue
1867 }
1868
1869 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001870 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001871
1872 if len(scopeProperties.Stub_srcs) > 0 {
1873 // Add dependencies to the prebuilt stubs source library
1874 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1875 }
Paul Duffin56d44902020-01-31 13:36:25 +00001876 }
Colin Cross79c7c262019-04-17 11:11:46 -07001877}
1878
Paul Duffin46fdda82020-05-14 15:39:10 +01001879func (module *sdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
1880 return module.commonOutputFiles(tag)
1881}
1882
Colin Cross79c7c262019-04-17 11:11:46 -07001883func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001884 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001885 ctx.VisitDirectDeps(func(to android.Module) {
1886 tag := ctx.OtherModuleDependencyTag(to)
1887
Paul Duffin533f9c72020-05-20 16:18:00 +01001888 // Extract information from any of the scope specific dependencies.
1889 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1890 apiScope := scopeTag.apiScope
1891 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1892
1893 // Extract information from the dependency. The exact information extracted
1894 // is determined by the nature of the dependency which is determined by the tag.
1895 scopeTag.extractDepInfo(ctx, to, scopePaths)
Colin Cross79c7c262019-04-17 11:11:46 -07001896 }
1897 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001898
1899 // Populate the scope paths with information from the properties.
1900 for apiScope, scopeProperties := range module.scopeProperties {
1901 if len(scopeProperties.Jars) == 0 {
1902 continue
1903 }
1904
1905 paths := module.getScopePathsCreateIfNeeded(apiScope)
1906 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1907 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1908 }
Colin Cross79c7c262019-04-17 11:11:46 -07001909}
1910
Paul Duffin47624362020-05-20 12:19:10 +01001911func (module *sdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffina3fb67d2020-05-20 14:20:02 +01001912 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001913}
1914
Colin Cross79c7c262019-04-17 11:11:46 -07001915// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001916func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001917 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001918 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001919}
1920
1921// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001922func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001923 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001924 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001925}
Jiyong Parke3833882020-02-17 17:28:10 +09001926
1927//
1928// java_sdk_library_xml
1929//
1930type sdkLibraryXml struct {
1931 android.ModuleBase
1932 android.DefaultableModuleBase
1933 android.ApexModuleBase
1934
1935 properties sdkLibraryXmlProperties
1936
1937 outputFilePath android.OutputPath
1938 installDirPath android.InstallPath
1939}
1940
1941type sdkLibraryXmlProperties struct {
1942 // canonical name of the lib
1943 Lib_name *string
1944}
1945
1946// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1947// Not to be used directly by users. java_sdk_library internally uses this.
1948func sdkLibraryXmlFactory() android.Module {
1949 module := &sdkLibraryXml{}
1950
1951 module.AddProperties(&module.properties)
1952
1953 android.InitApexModule(module)
1954 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1955
1956 return module
1957}
1958
1959// from android.PrebuiltEtcModule
1960func (module *sdkLibraryXml) SubDir() string {
1961 return "permissions"
1962}
1963
1964// from android.PrebuiltEtcModule
1965func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1966 return module.outputFilePath
1967}
1968
1969// from android.ApexModule
1970func (module *sdkLibraryXml) AvailableFor(what string) bool {
1971 return true
1972}
1973
1974func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1975 // do nothing
1976}
1977
1978// File path to the runtime implementation library
1979func (module *sdkLibraryXml) implPath() string {
1980 implName := proptools.String(module.properties.Lib_name)
1981 if apexName := module.ApexName(); apexName != "" {
1982 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1983 // In most cases, this works fine. But when apex_name is set or override_apex is used
1984 // this can be wrong.
1985 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1986 }
1987 partition := "system"
1988 if module.SocSpecific() {
1989 partition = "vendor"
1990 } else if module.DeviceSpecific() {
1991 partition = "odm"
1992 } else if module.ProductSpecific() {
1993 partition = "product"
1994 } else if module.SystemExtSpecific() {
1995 partition = "system_ext"
1996 }
1997 return "/" + partition + "/framework/" + implName + ".jar"
1998}
1999
2000func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2001 libName := proptools.String(module.properties.Lib_name)
2002 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2003
2004 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2005 rule := android.NewRuleBuilder()
2006 rule.Command().
2007 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2008 Output(module.outputFilePath)
2009
2010 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2011
2012 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2013}
2014
2015func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2016 if !module.IsForPlatform() {
2017 return []android.AndroidMkEntries{android.AndroidMkEntries{
2018 Disabled: true,
2019 }}
2020 }
2021
2022 return []android.AndroidMkEntries{android.AndroidMkEntries{
2023 Class: "ETC",
2024 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2025 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2026 func(entries *android.AndroidMkEntries) {
2027 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2028 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2029 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2030 },
2031 },
2032 }}
2033}
Paul Duffin61871622020-02-10 13:37:10 +00002034
2035type sdkLibrarySdkMemberType struct {
2036 android.SdkMemberTypeBase
2037}
2038
2039func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2040 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2041}
2042
2043func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2044 _, ok := module.(*SdkLibrary)
2045 return ok
2046}
2047
2048func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2049 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2050}
2051
2052func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2053 return &sdkLibrarySdkMemberProperties{}
2054}
2055
2056type sdkLibrarySdkMemberProperties struct {
2057 android.SdkMemberPropertiesBase
2058
2059 // Scope to per scope properties.
2060 Scopes map[*apiScope]scopeProperties
2061
2062 // Additional libraries that the exported stubs libraries depend upon.
2063 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01002064
2065 // The Java stubs source files.
2066 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01002067
2068 // The naming scheme.
2069 Naming_scheme *string
Paul Duffina84756c2020-05-26 20:57:10 +01002070
2071 // True if the java_sdk_library_import is for a shared library, false
2072 // otherwise.
2073 Shared_library *bool
Paul Duffin61871622020-02-10 13:37:10 +00002074}
2075
2076type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01002077 Jars android.Paths
2078 StubsSrcJar android.Path
2079 CurrentApiFile android.Path
2080 RemovedApiFile android.Path
2081 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00002082}
2083
2084func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2085 sdk := variant.(*SdkLibrary)
2086
2087 s.Scopes = make(map[*apiScope]scopeProperties)
2088 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01002089 paths := sdk.findScopePaths(apiScope)
2090 if paths == nil {
2091 continue
2092 }
2093
Paul Duffin61871622020-02-10 13:37:10 +00002094 jars := paths.stubsImplPath
2095 if len(jars) > 0 {
2096 properties := scopeProperties{}
2097 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01002098 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01002099 properties.StubsSrcJar = paths.stubsSrcJar.Path()
2100 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2101 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffin61871622020-02-10 13:37:10 +00002102 s.Scopes[apiScope] = properties
2103 }
2104 }
2105
2106 s.Libs = sdk.properties.Libs
Paul Duffind11e78e2020-05-15 20:37:11 +01002107 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffina84756c2020-05-26 20:57:10 +01002108 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin61871622020-02-10 13:37:10 +00002109}
2110
2111func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01002112 if s.Naming_scheme != nil {
2113 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2114 }
Paul Duffina84756c2020-05-26 20:57:10 +01002115 if s.Shared_library != nil {
2116 propertySet.AddProperty("shared_library", *s.Shared_library)
2117 }
Paul Duffinf8e08b22020-05-13 16:54:55 +01002118
Paul Duffin61871622020-02-10 13:37:10 +00002119 for _, apiScope := range allApiScopes {
2120 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01002121 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00002122
Paul Duffinf488ef22020-04-09 00:10:17 +01002123 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2124
Paul Duffin61871622020-02-10 13:37:10 +00002125 var jars []string
2126 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01002127 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00002128 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2129 jars = append(jars, dest)
2130 }
2131 scopeSet.AddProperty("jars", jars)
2132
Paul Duffinf488ef22020-04-09 00:10:17 +01002133 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2134 // the source files are also unpacked.
2135 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2136 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2137 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2138
Paul Duffin75dcc802020-04-09 01:08:11 +01002139 if properties.CurrentApiFile != nil {
2140 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2141 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2142 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2143 }
2144
2145 if properties.RemovedApiFile != nil {
2146 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffinb1787352020-06-02 13:00:02 +01002147 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin75dcc802020-04-09 01:08:11 +01002148 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2149 }
2150
Paul Duffin61871622020-02-10 13:37:10 +00002151 if properties.SdkVersion != "" {
2152 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2153 }
2154 }
2155 }
2156
2157 if len(s.Libs) > 0 {
2158 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2159 }
2160}