blob: 17217ed41337055c5c31cfa8724c3e3a99abe289 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin6a2bd112020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46fdda82020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin6a2bd112020-04-07 19:27:04 +010029
30 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090031)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Paul Duffin1c094a02020-05-08 15:52:37 +010034 sdkXmlFileSuffix = ".xml"
35 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090036 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090038 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 ` you may not use this file except in compliance with the License.\n` +
40 ` You may obtain a copy of the License at\n` +
41 `\n` +
42 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
43 `\n` +
44 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090045 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090046 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47 ` See the License for the specific language governing permissions and\n` +
48 ` limitations under the License.\n` +
49 `-->\n` +
50 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090051 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090052 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090053)
54
Paul Duffind1b3a922020-01-22 11:57:20 +000055// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57 blueprint.BaseDependencyTag
58 name string
59 apiScope *apiScope
Paul Duffin5fb82132020-04-29 20:45:27 +010060
61 // Function for extracting appropriate path information from the dependency.
62 depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67 err := tag.depInfoExtractor(paths, dep)
68 if err != nil {
69 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70 }
Paul Duffind1b3a922020-01-22 11:57:20 +000071}
72
73// Provides information about an api scope, e.g. public, system, test.
74type apiScope struct {
75 // The name of the api scope, e.g. public, system, test
76 name string
77
Paul Duffin51a2bee2020-05-05 14:40:52 +010078 // The api scope that this scope extends.
79 extends *apiScope
80
Paul Duffin3a254982020-04-28 10:44:03 +010081 // The legacy enabled status for a specific scope can be dependent on other
82 // properties that have been specified on the library so it is provided by
83 // a function that can determine the status by examining those properties.
84 legacyEnabledStatus func(module *SdkLibrary) bool
85
86 // The default enabled status for non-legacy behavior, which is triggered by
87 // explicitly enabling at least one api scope.
88 defaultEnabledStatus bool
89
90 // Gets a pointer to the scope specific properties.
91 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
92
Paul Duffin6a2bd112020-04-07 19:27:04 +010093 // The name of the field in the dynamically created structure.
94 fieldName string
95
Paul Duffin0f270632020-05-13 19:19:49 +010096 // The name of the property in the java_sdk_library_import
97 propertyName string
98
Paul Duffind1b3a922020-01-22 11:57:20 +000099 // The tag to use to depend on the stubs library module.
100 stubsTag scopeDependencyTag
101
Paul Duffina377e4c2020-04-29 13:30:54 +0100102 // The tag to use to depend on the stubs source module (if separate from the API module).
103 stubsSourceTag scopeDependencyTag
104
105 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
106 apiFileTag scopeDependencyTag
107
Paul Duffin5fb82132020-04-29 20:45:27 +0100108 // The tag to use to depend on the stubs source and API module.
109 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
111 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
112 apiFilePrefix string
113
114 // The scope specific prefix to add to the sdk library module name to construct a scope specific
115 // module name.
116 moduleSuffix string
117
Paul Duffind1b3a922020-01-22 11:57:20 +0000118 // SDK version that the stubs library is built against. Note that this is always
119 // *current. Older stubs library built with a numbered SDK version is created from
120 // the prebuilt jar.
121 sdkVersion string
Paul Duffin3c7c3472020-04-07 18:50:10 +0100122
123 // Extra arguments to pass to droidstubs for this scope.
124 droidstubsArgs []string
Anton Hansson5ff28e52020-05-02 11:19:36 +0100125
Paul Duffina377e4c2020-04-29 13:30:54 +0100126 // The args that must be passed to droidstubs to generate the stubs source
127 // for this scope.
128 //
129 // The stubs source must include the definitions of everything that is in this
130 // api scope and all the scopes that this one extends.
131 droidstubsArgsForGeneratingStubsSource []string
132
133 // The args that must be passed to droidstubs to generate the API for this scope.
134 //
135 // The API only includes the additional members that this scope adds over the scope
136 // that it extends.
137 droidstubsArgsForGeneratingApi []string
138
139 // True if the stubs source and api can be created by the same metalava invocation.
140 createStubsSourceAndApiTogether bool
141
Anton Hansson5ff28e52020-05-02 11:19:36 +0100142 // Whether the api scope can be treated as unstable, and should skip compat checks.
143 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000144}
145
146// Initialize a scope, creating and adding appropriate dependency tags
147func initApiScope(scope *apiScope) *apiScope {
Paul Duffin5fb82132020-04-29 20:45:27 +0100148 name := scope.name
Paul Duffin46fdda82020-05-14 15:39:10 +0100149 scopeByName[name] = scope
150 allScopeNames = append(allScopeNames, name)
Paul Duffin0f270632020-05-13 19:19:49 +0100151 scope.propertyName = strings.ReplaceAll(name, "-", "_")
152 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000153 scope.stubsTag = scopeDependencyTag{
Paul Duffin5fb82132020-04-29 20:45:27 +0100154 name: name + "-stubs",
155 apiScope: scope,
156 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000157 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100158 scope.stubsSourceTag = scopeDependencyTag{
159 name: name + "-stubs-source",
160 apiScope: scope,
161 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
162 }
163 scope.apiFileTag = scopeDependencyTag{
164 name: name + "-api",
165 apiScope: scope,
166 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
167 }
Paul Duffin5fb82132020-04-29 20:45:27 +0100168 scope.stubsSourceAndApiTag = scopeDependencyTag{
169 name: name + "-stubs-source-and-api",
170 apiScope: scope,
171 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000172 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100173
174 // To get the args needed to generate the stubs source append all the args from
175 // this scope and all the scopes it extends as each set of args adds additional
176 // members to the stubs.
177 var stubsSourceArgs []string
178 for s := scope; s != nil; s = s.extends {
179 stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
180 }
181 scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
182
183 // Currently the args needed to generate the API are the same as the args
184 // needed to add additional members.
185 apiArgs := scope.droidstubsArgs
186 scope.droidstubsArgsForGeneratingApi = apiArgs
187
188 // If the args needed to generate the stubs and API are the same then they
189 // can be generated in a single invocation of metalava, otherwise they will
190 // need separate invocations.
191 scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
192
Paul Duffind1b3a922020-01-22 11:57:20 +0000193 return scope
194}
195
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100196func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100197 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000198}
199
Paul Duffin5fb82132020-04-29 20:45:27 +0100200func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100201 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000202}
203
Paul Duffina377e4c2020-04-29 13:30:54 +0100204func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100205 return baseName + ".api" + scope.moduleSuffix
Paul Duffina377e4c2020-04-29 13:30:54 +0100206}
207
Paul Duffin3a254982020-04-28 10:44:03 +0100208func (scope *apiScope) String() string {
209 return scope.name
210}
211
Paul Duffind1b3a922020-01-22 11:57:20 +0000212type apiScopes []*apiScope
213
214func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
215 var list []string
216 for _, scope := range scopes {
217 list = append(list, accessor(scope))
218 }
219 return list
220}
221
Jiyong Parkc678ad32018-04-10 13:07:10 +0900222var (
Paul Duffin46fdda82020-05-14 15:39:10 +0100223 scopeByName = make(map[string]*apiScope)
224 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000225 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100226 name: "public",
227
228 // Public scope is enabled by default for both legacy and non-legacy modes.
229 legacyEnabledStatus: func(module *SdkLibrary) bool {
230 return true
231 },
232 defaultEnabledStatus: true,
233
234 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
235 return &module.sdkLibraryProperties.Public
236 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000237 sdkVersion: "current",
238 })
239 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100240 name: "system",
241 extends: apiScopePublic,
242 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
243 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
244 return &module.sdkLibraryProperties.System
245 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100246 apiFilePrefix: "system-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100247 moduleSuffix: ".system",
Anton Hanssone366fff2020-04-28 16:47:41 +0100248 sdkVersion: "system_current",
Paul Duffin991f2622020-04-29 22:18:41 +0100249 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000250 })
251 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100252 name: "test",
253 extends: apiScopePublic,
254 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
255 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
256 return &module.sdkLibraryProperties.Test
257 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100258 apiFilePrefix: "test-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100259 moduleSuffix: ".test",
Anton Hanssone366fff2020-04-28 16:47:41 +0100260 sdkVersion: "test_current",
261 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100262 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000263 })
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100264 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin0f270632020-05-13 19:19:49 +0100265 name: "module-lib",
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100266 extends: apiScopeSystem,
267 // Module_lib scope is disabled by default in legacy mode.
268 //
269 // Enabling this would break existing usages.
270 legacyEnabledStatus: func(module *SdkLibrary) bool {
271 return false
272 },
273 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
274 return &module.sdkLibraryProperties.Module_lib
275 },
276 apiFilePrefix: "module-lib-",
277 moduleSuffix: ".module_lib",
278 sdkVersion: "module_current",
279 droidstubsArgs: []string{
280 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
281 },
282 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000283 allApiScopes = apiScopes{
284 apiScopePublic,
285 apiScopeSystem,
286 apiScopeTest,
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100287 apiScopeModuleLib,
Paul Duffind1b3a922020-01-22 11:57:20 +0000288 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900289)
290
Jiyong Park82484c02018-04-23 21:41:26 +0900291var (
292 javaSdkLibrariesLock sync.Mutex
293)
294
Jiyong Parkc678ad32018-04-10 13:07:10 +0900295// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900296// 1) disallowing linking to the runtime shared lib
297// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900298
299func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000300 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900301
Jiyong Park82484c02018-04-23 21:41:26 +0900302 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
303 javaSdkLibraries := javaSdkLibraries(ctx.Config())
304 sort.Strings(*javaSdkLibraries)
305 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
306 })
Paul Duffin61871622020-02-10 13:37:10 +0000307
308 // Register sdk member types.
309 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
310 android.SdkMemberTypeBase{
311 PropertyName: "java_sdk_libs",
312 SupportsSdk: true,
313 },
314 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900315}
316
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000317func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
318 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
319 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
320}
321
Paul Duffin3a254982020-04-28 10:44:03 +0100322// Properties associated with each api scope.
323type ApiScopeProperties struct {
324 // Indicates whether the api surface is generated.
325 //
326 // If this is set for any scope then all scopes must explicitly specify if they
327 // are enabled. This is to prevent new usages from depending on legacy behavior.
328 //
329 // Otherwise, if this is not set for any scope then the default behavior is
330 // scope specific so please refer to the scope specific property documentation.
331 Enabled *bool
Paul Duffin080f5ee2020-05-12 11:50:28 +0100332
333 // The sdk_version to use for building the stubs.
334 //
335 // If not specified then it will use an sdk_version determined as follows:
336 // 1) If the sdk_version specified on the java_sdk_library is none then this
337 // will be none. This is used for java_sdk_library instances that are used
338 // to create stubs that contribute to the core_current sdk version.
339 // 2) Otherwise, it is assumed that this library extends but does not contribute
340 // directly to a specific sdk_version and so this uses the sdk_version appropriate
341 // for the api scope. e.g. public will use sdk_version: current, system will use
342 // sdk_version: system_current, etc.
343 //
344 // This does not affect the sdk_version used for either generating the stubs source
345 // or the API file. They both have to use the same sdk_version as is used for
346 // compiling the implementation library.
347 Sdk_version *string
Paul Duffin3a254982020-04-28 10:44:03 +0100348}
349
Jiyong Parkc678ad32018-04-10 13:07:10 +0900350type sdkLibraryProperties struct {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100351 // Visibility for impl library module. If not specified then defaults to the
352 // visibility property.
353 Impl_library_visibility []string
354
Paul Duffin344c4ee2020-04-29 23:35:13 +0100355 // Visibility for stubs library modules. If not specified then defaults to the
356 // visibility property.
357 Stubs_library_visibility []string
358
359 // Visibility for stubs source modules. If not specified then defaults to the
360 // visibility property.
361 Stubs_source_visibility []string
362
Sundong Ahnf043cf62018-06-25 16:04:37 +0900363 // List of Java libraries that will be in the classpath when building stubs
364 Stub_only_libs []string `android:"arch_variant"`
365
Paul Duffin7a586d32019-12-30 17:09:34 +0000366 // list of package names that will be documented and publicized as API.
367 // This allows the API to be restricted to a subset of the source files provided.
368 // If this is unspecified then all the source files will be treated as being part
369 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900370 Api_packages []string
371
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900372 // list of package names that must be hidden from the API
373 Hidden_api_packages []string
374
Paul Duffin749f98f2019-12-30 17:23:46 +0000375 // the relative path to the directory containing the api specification files.
376 // Defaults to "api".
377 Api_dir *string
378
Paul Duffind11e78e2020-05-15 20:37:11 +0100379 // Determines whether a runtime implementation library is built; defaults to false.
380 //
381 // If true then it also prevents the module from being used as a shared module, i.e.
382 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000383 Api_only *bool
384
Paul Duffin11512472019-02-11 15:55:17 +0000385 // local files that are used within user customized droiddoc options.
386 Droiddoc_option_files []string
387
388 // additional droiddoc options
389 // Available variables for substitution:
390 //
391 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900392 Droiddoc_options []string
393
Paul Duffin2ce1e812020-05-20 19:35:27 +0100394 // is set to true, Metalava will allow framework SDK to contain annotations.
395 Annotations_enabled *bool
396
Sundong Ahn054b19a2018-10-19 13:46:09 +0900397 // a list of top-level directories containing files to merge qualifier annotations
398 // (i.e. those intended to be included in the stubs written) from.
399 Merge_annotations_dirs []string
400
401 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
402 Merge_inclusion_annotations_dirs []string
403
404 // If set to true, the path of dist files is apistubs/core. Defaults to false.
405 Core_lib *bool
406
Sundong Ahn80a87b32019-05-13 15:02:50 +0900407 // don't create dist rules.
408 No_dist *bool `blueprint:"mutated"`
409
Paul Duffin3a254982020-04-28 10:44:03 +0100410 // indicates whether system and test apis should be generated.
411 Generate_system_and_test_apis bool `blueprint:"mutated"`
412
413 // The properties specific to the public api scope
414 //
415 // Unless explicitly specified by using public.enabled the public api scope is
416 // enabled by default in both legacy and non-legacy mode.
417 Public ApiScopeProperties
418
419 // The properties specific to the system api scope
420 //
421 // In legacy mode the system api scope is enabled by default when sdk_version
422 // is set to something other than "none".
423 //
424 // In non-legacy mode the system api scope is disabled by default.
425 System ApiScopeProperties
426
427 // The properties specific to the test api scope
428 //
429 // In legacy mode the test api scope is enabled by default when sdk_version
430 // is set to something other than "none".
431 //
432 // In non-legacy mode the test api scope is disabled by default.
433 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000434
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100435 // The properties specific to the module_lib api scope
436 //
437 // Unless explicitly specified by using test.enabled the module_lib api scope is
438 // disabled by default.
439 Module_lib ApiScopeProperties
440
Paul Duffin8986cc92020-05-10 19:32:20 +0100441 // Properties related to api linting.
442 Api_lint struct {
443 // Enable api linting.
444 Enabled *bool
445 }
446
Jiyong Parkc678ad32018-04-10 13:07:10 +0900447 // TODO: determines whether to create HTML doc or not
448 //Html_doc *bool
449}
450
Paul Duffin533f9c72020-05-20 16:18:00 +0100451// Paths to outputs from java_sdk_library and java_sdk_library_import.
452//
453// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
454// OptionalPaths are always set by java_sdk_library but may not be set by
455// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000456type scopePaths struct {
Paul Duffin533f9c72020-05-20 16:18:00 +0100457 // The path (represented as Paths for convenience when returning) to the stubs header jar.
458 //
459 // That is the jar that is created by turbine.
460 stubsHeaderPath android.Paths
461
462 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
463 //
464 // This is not the implementation jar, it still only contains stubs.
465 stubsImplPath android.Paths
466
467 // The API specification file, e.g. system_current.txt.
468 currentApiFilePath android.OptionalPath
469
470 // The specification of API elements removed since the last release.
471 removedApiFilePath android.OptionalPath
472
473 // The stubs source jar.
474 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000475}
476
Paul Duffin5fb82132020-04-29 20:45:27 +0100477func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
478 if lib, ok := dep.(Dependency); ok {
479 paths.stubsHeaderPath = lib.HeaderJars()
480 paths.stubsImplPath = lib.ImplementationJars()
481 return nil
482 } else {
483 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
484 }
485}
486
Paul Duffina377e4c2020-04-29 13:30:54 +0100487func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
488 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
489 action(apiStubsProvider)
Paul Duffin5fb82132020-04-29 20:45:27 +0100490 return nil
491 } else {
492 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
493 }
494}
495
Paul Duffin533f9c72020-05-20 16:18:00 +0100496func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
497 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
498 action(apiStubsProvider)
499 return nil
500 } else {
501 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
502 }
503}
504
Paul Duffina377e4c2020-04-29 13:30:54 +0100505func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin533f9c72020-05-20 16:18:00 +0100506 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
507 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffina377e4c2020-04-29 13:30:54 +0100508}
509
510func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
511 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
512 paths.extractApiInfoFromApiStubsProvider(provider)
513 })
514}
515
Paul Duffin533f9c72020-05-20 16:18:00 +0100516func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
517 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffina377e4c2020-04-29 13:30:54 +0100518}
519
520func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin533f9c72020-05-20 16:18:00 +0100521 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffina377e4c2020-04-29 13:30:54 +0100522 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
523 })
524}
525
526func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
527 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
528 paths.extractApiInfoFromApiStubsProvider(provider)
529 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
530 })
531}
532
533type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1a724e62020-05-08 13:44:43 +0100534 // The naming scheme to use for the components that this module creates.
535 //
Paul Duffindef8a892020-05-08 15:36:30 +0100536 // If not specified then it defaults to "default". The other allowable value is
537 // "framework-modules" which matches the scheme currently used by framework modules
538 // for the equivalent components represented as separate Soong modules.
Paul Duffin1a724e62020-05-08 13:44:43 +0100539 //
540 // This is a temporary mechanism to simplify conversion from separate modules for each
541 // component that follow a different naming pattern to the default one.
542 //
543 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffina377e4c2020-04-29 13:30:54 +0100544 Naming_scheme *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100545
546 // Specifies whether this module can be used as an Android shared library; defaults
547 // to true.
548 //
549 // An Android shared library is one that can be referenced in a <uses-library> element
550 // in an AndroidManifest.xml.
551 Shared_library *bool
Paul Duffina377e4c2020-04-29 13:30:54 +0100552}
553
Paul Duffin56d44902020-01-31 13:36:25 +0000554// Common code between sdk library and sdk library import
555type commonToSdkLibraryAndImport struct {
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100556 moduleBase *android.ModuleBase
557
Paul Duffin56d44902020-01-31 13:36:25 +0000558 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100559
560 namingScheme sdkLibraryComponentNamingScheme
561
Paul Duffind11e78e2020-05-15 20:37:11 +0100562 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin64e61992020-05-15 10:20:31 +0100563
564 // Functionality related to this being used as a component of a java_sdk_library.
565 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000566}
567
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100568func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
569 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100570
Paul Duffind11e78e2020-05-15 20:37:11 +0100571 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin64e61992020-05-15 10:20:31 +0100572
573 // Initialize this as an sdk library component.
574 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1a724e62020-05-08 13:44:43 +0100575}
576
577func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffind11e78e2020-05-15 20:37:11 +0100578 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1a724e62020-05-08 13:44:43 +0100579 switch schemeProperty {
580 case "default":
581 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100582 case "framework-modules":
583 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100584 default:
585 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
586 return false
587 }
588
Paul Duffind11e78e2020-05-15 20:37:11 +0100589 // Only track this sdk library if this can be used as a shared library.
590 if c.sharedLibrary() {
591 // Use the name specified in the module definition as the owner.
592 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
593 }
Paul Duffin64e61992020-05-15 10:20:31 +0100594
Paul Duffin1a724e62020-05-08 13:44:43 +0100595 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100596}
597
598// Name of the java_library module that compiles the stubs source.
599func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100600 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100601}
602
603// Name of the droidstubs module that generates the stubs source and may also
604// generate/check the API.
605func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100606 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100607}
608
609// Name of the droidstubs module that generates/checks the API. Only used if it
610// requires different arts to the stubs source generating module.
611func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100612 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100613}
614
Paul Duffin46fdda82020-05-14 15:39:10 +0100615// The component names for different outputs of the java_sdk_library.
616//
617// They are similar to the names used for the child modules it creates
618const (
619 stubsSourceComponentName = "stubs.source"
620
621 apiTxtComponentName = "api.txt"
622
623 removedApiTxtComponentName = "removed-api.txt"
624)
625
626// A regular expression to match tags that reference a specific stubs component.
627//
628// It will only match if given a valid scope and a valid component. It is verfy strict
629// to ensure it does not accidentally match a similar looking tag that should be processed
630// by the embedded Library.
631var tagSplitter = func() *regexp.Regexp {
632 // Given a list of literal string items returns a regular expression that will
633 // match any one of the items.
634 choice := func(items ...string) string {
635 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
636 }
637
638 // Regular expression to match one of the scopes.
639 scopesRegexp := choice(allScopeNames...)
640
641 // Regular expression to match one of the components.
642 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
643
644 // Regular expression to match any combination of one scope and one component.
645 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
646}()
647
648// For OutputFileProducer interface
649//
650// .<scope>.stubs.source
651// .<scope>.api.txt
652// .<scope>.removed-api.txt
653func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
654 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
655 scopeName := groups[1]
656 component := groups[2]
657
658 if scope, ok := scopeByName[scopeName]; ok {
659 paths := c.findScopePaths(scope)
660 if paths == nil {
661 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
662 }
663
664 switch component {
665 case stubsSourceComponentName:
666 if paths.stubsSrcJar.Valid() {
667 return android.Paths{paths.stubsSrcJar.Path()}, nil
668 }
669
670 case apiTxtComponentName:
671 if paths.currentApiFilePath.Valid() {
672 return android.Paths{paths.currentApiFilePath.Path()}, nil
673 }
674
675 case removedApiTxtComponentName:
676 if paths.removedApiFilePath.Valid() {
677 return android.Paths{paths.removedApiFilePath.Path()}, nil
678 }
679 }
680
681 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
682 } else {
683 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
684 }
685
686 } else {
687 return nil, nil
688 }
689}
690
Paul Duffin5ae30792020-05-20 11:52:25 +0100691func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000692 if c.scopePaths == nil {
693 c.scopePaths = make(map[*apiScope]*scopePaths)
694 }
695 paths := c.scopePaths[scope]
696 if paths == nil {
697 paths = &scopePaths{}
698 c.scopePaths[scope] = paths
699 }
700
701 return paths
702}
703
Paul Duffin5ae30792020-05-20 11:52:25 +0100704func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
705 if c.scopePaths == nil {
706 return nil
707 }
708
709 return c.scopePaths[scope]
710}
711
712// If this does not support the requested api scope then find the closest available
713// scope it does support. Returns nil if no such scope is available.
714func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
715 for s := scope; s != nil; s = s.extends {
716 if paths := c.findScopePaths(s); paths != nil {
717 return paths
718 }
719 }
720
721 // This should never happen outside tests as public should be the base scope for every
722 // scope and is enabled by default.
723 return nil
724}
725
Paul Duffina3fb67d2020-05-20 14:20:02 +0100726func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-05-20 12:19:10 +0100727
728 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
729 if sdkVersion.version.isNumbered() {
730 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
731 }
732
733 var apiScope *apiScope
734 switch sdkVersion.kind {
735 case sdkSystem:
736 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100737 case sdkModule:
738 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100739 case sdkTest:
740 apiScope = apiScopeTest
741 default:
742 apiScope = apiScopePublic
743 }
744
Paul Duffin5ae30792020-05-20 11:52:25 +0100745 paths := c.findClosestScopePath(apiScope)
746 if paths == nil {
747 var scopes []string
748 for _, s := range allApiScopes {
749 if c.findScopePaths(s) != nil {
750 scopes = append(scopes, s.name)
751 }
752 }
753 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
754 return nil
755 }
756
Paul Duffina3fb67d2020-05-20 14:20:02 +0100757 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100758}
759
Paul Duffin64e61992020-05-15 10:20:31 +0100760func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
761 componentProps := &struct {
762 SdkLibraryToImplicitlyTrack *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100763 }{}
764
765 if c.sharedLibrary() {
Paul Duffin64e61992020-05-15 10:20:31 +0100766 // Mark the stubs library as being components of this java_sdk_library so that
767 // any app that includes code which depends (directly or indirectly) on the stubs
768 // library will have the appropriate <uses-library> invocation inserted into its
769 // manifest if necessary.
Paul Duffind11e78e2020-05-15 20:37:11 +0100770 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin64e61992020-05-15 10:20:31 +0100771 }
772
773 return componentProps
774}
775
Paul Duffind11e78e2020-05-15 20:37:11 +0100776// Check if this can be used as a shared library.
777func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
778 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
779}
780
Paul Duffin64e61992020-05-15 10:20:31 +0100781// Properties related to the use of a module as an component of a java_sdk_library.
782type SdkLibraryComponentProperties struct {
783
784 // The name of the java_sdk_library/_import to add to a <uses-library> entry
785 // in the AndroidManifest.xml of any Android app that includes code that references
786 // this module. If not set then no java_sdk_library/_import is tracked.
787 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
788}
789
790// Structure to be embedded in a module struct that needs to support the
791// SdkLibraryComponentDependency interface.
792type EmbeddableSdkLibraryComponent struct {
793 sdkLibraryComponentProperties SdkLibraryComponentProperties
794}
795
796func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
797 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
798}
799
800// to satisfy SdkLibraryComponentDependency
801func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
802 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
803 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
804 }
805 return nil
806}
807
808// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
809// (including the java_sdk_library) itself.
810type SdkLibraryComponentDependency interface {
811 // The optional name of the sdk library that should be implicitly added to the
812 // AndroidManifest of an app that contains code which references the sdk library.
813 //
814 // Returns an array containing 0 or 1 items rather than a *string to make it easier
815 // to append this to the list of exported sdk libraries.
816 OptionalImplicitSdkLibrary() []string
817}
818
819// Make sure that all the module types that are components of java_sdk_library/_import
820// and which can be referenced (directly or indirectly) from an android app implement
821// the SdkLibraryComponentDependency interface.
822var _ SdkLibraryComponentDependency = (*Library)(nil)
823var _ SdkLibraryComponentDependency = (*Import)(nil)
824var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
825var _ SdkLibraryComponentDependency = (*sdkLibraryImport)(nil)
826
827// Provides access to sdk_version related header and implentation jars.
828type SdkLibraryDependency interface {
829 SdkLibraryComponentDependency
830
831 // Get the header jars appropriate for the supplied sdk_version.
832 //
833 // These are turbine generated jars so they only change if the externals of the
834 // class changes but it does not contain and implementation or JavaDoc.
835 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
836
837 // Get the implementation jars appropriate for the supplied sdk version.
838 //
839 // These are either the implementation jar for the whole sdk library or the implementation
840 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
841 // they are identical to the corresponding header jars.
842 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
843}
844
Inseob Kimc0907f12019-02-08 21:00:45 +0900845type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900846 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900847
Sundong Ahn054b19a2018-10-19 13:46:09 +0900848 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900849
Paul Duffin3a254982020-04-28 10:44:03 +0100850 // Map from api scope to the scope specific property structure.
851 scopeToProperties map[*apiScope]*ApiScopeProperties
852
Paul Duffin56d44902020-01-31 13:36:25 +0000853 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900854}
855
Inseob Kimc0907f12019-02-08 21:00:45 +0900856var _ Dependency = (*SdkLibrary)(nil)
857var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800858
Paul Duffin3a254982020-04-28 10:44:03 +0100859func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
860 return module.sdkLibraryProperties.Generate_system_and_test_apis
861}
862
863func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
864 // Check to see if any scopes have been explicitly enabled. If any have then all
865 // must be.
866 anyScopesExplicitlyEnabled := false
867 for _, scope := range allApiScopes {
868 scopeProperties := module.scopeToProperties[scope]
869 if scopeProperties.Enabled != nil {
870 anyScopesExplicitlyEnabled = true
871 break
872 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000873 }
Paul Duffin3a254982020-04-28 10:44:03 +0100874
875 var generatedScopes apiScopes
876 enabledScopes := make(map[*apiScope]struct{})
877 for _, scope := range allApiScopes {
878 scopeProperties := module.scopeToProperties[scope]
879 // If any scopes are explicitly enabled then ignore the legacy enabled status.
880 // This is to ensure that any new usages of this module type do not rely on legacy
881 // behaviour.
882 defaultEnabledStatus := false
883 if anyScopesExplicitlyEnabled {
884 defaultEnabledStatus = scope.defaultEnabledStatus
885 } else {
886 defaultEnabledStatus = scope.legacyEnabledStatus(module)
887 }
888 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
889 if enabled {
890 enabledScopes[scope] = struct{}{}
891 generatedScopes = append(generatedScopes, scope)
892 }
893 }
894
895 // Now check to make sure that any scope that is extended by an enabled scope is also
896 // enabled.
897 for _, scope := range allApiScopes {
898 if _, ok := enabledScopes[scope]; ok {
899 extends := scope.extends
900 if extends != nil {
901 if _, ok := enabledScopes[extends]; !ok {
902 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
903 }
904 }
905 }
906 }
907
908 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000909}
910
Paul Duffine74ac732020-02-06 13:51:46 +0000911var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
912
Jiyong Parke3833882020-02-17 17:28:10 +0900913func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
914 if dt, ok := depTag.(dependencyTag); ok {
915 return dt == xmlPermissionsFileTag
916 }
917 return false
918}
919
Paul Duffin9d582cc2020-05-16 15:52:12 +0100920var implLibraryTag = dependencyTag{name: "impl-library"}
921
Inseob Kimc0907f12019-02-08 21:00:45 +0900922func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100923 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000924 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100925 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000926
Paul Duffina377e4c2020-04-29 13:30:54 +0100927 // If the stubs source and API cannot be generated together then add an additional dependency on
928 // the API module.
929 if apiScope.createStubsSourceAndApiTogether {
930 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100931 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100932 } else {
933 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100934 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
935 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100936 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900937 }
938
Paul Duffind11e78e2020-05-15 20:37:11 +0100939 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100940 // Add dependency to the rule for generating the implementation library.
941 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
942
Paul Duffind11e78e2020-05-15 20:37:11 +0100943 if module.sharedLibrary() {
944 // Add dependency to the rule for generating the xml permissions file
945 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
946 }
Paul Duffine74ac732020-02-06 13:51:46 +0000947
Paul Duffind11e78e2020-05-15 20:37:11 +0100948 // Only add the deps for the library if it is actually going to be built.
949 module.Library.deps(ctx)
950 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900951}
952
Paul Duffin46fdda82020-05-14 15:39:10 +0100953func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
954 paths, err := module.commonOutputFiles(tag)
955 if paths == nil && err == nil {
956 return module.Library.OutputFiles(tag)
957 } else {
958 return paths, err
959 }
960}
961
Inseob Kimc0907f12019-02-08 21:00:45 +0900962func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +0100963 // Only build an implementation library if required.
964 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +0000965 module.Library.GenerateAndroidBuildActions(ctx)
966 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900967
Sundong Ahn57368eb2018-07-06 11:20:23 +0900968 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000969 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900970 // the recorded paths will be returned depending on the link type of the caller.
971 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900972 tag := ctx.OtherModuleDependencyTag(to)
973
Paul Duffin5fb82132020-04-29 20:45:27 +0100974 // Extract information from any of the scope specific dependencies.
975 if scopeTag, ok := tag.(scopeDependencyTag); ok {
976 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +0100977 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +0100978
979 // Extract information from the dependency. The exact information extracted
980 // is determined by the nature of the dependency which is determined by the tag.
981 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900982 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900983 })
984}
985
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900986func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffind11e78e2020-05-15 20:37:11 +0100987 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +0000988 return nil
989 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900990 entriesList := module.Library.AndroidMkEntries()
991 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700992 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900993 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900994}
995
Jiyong Parkc678ad32018-04-10 13:07:10 +0900996// Module name of the runtime implementation library
Paul Duffin9d582cc2020-05-16 15:52:12 +0100997func (module *SdkLibrary) implLibraryModuleName() string {
998 return module.BaseModuleName() + ".impl"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900999}
1000
Jiyong Parkc678ad32018-04-10 13:07:10 +09001001// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +09001002func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001003 return module.BaseModuleName() + sdkXmlFileSuffix
1004}
1005
Anton Hansson6bb88102020-03-27 19:43:19 +00001006// The dist path of the stub artifacts
1007func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1008 if module.ModuleBase.Owner() != "" {
1009 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1010 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1011 return path.Join("apistubs", "core", apiScope.name)
1012 } else {
1013 return path.Join("apistubs", "android", apiScope.name)
1014 }
1015}
1016
Paul Duffin12ceb462019-12-24 20:31:31 +00001017// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +01001018func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +01001019 scopeProperties := module.scopeToProperties[apiScope]
1020 if scopeProperties.Sdk_version != nil {
1021 return proptools.String(scopeProperties.Sdk_version)
1022 }
1023
Paul Duffin12ceb462019-12-24 20:31:31 +00001024 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1025 if sdkDep.hasStandardLibs() {
1026 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001027 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001028 } else {
1029 // Otherwise, use no system module.
1030 return "none"
1031 }
1032}
1033
Paul Duffind1b3a922020-01-22 11:57:20 +00001034func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1035 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001036}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001037
Paul Duffind1b3a922020-01-22 11:57:20 +00001038func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1039 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001040}
1041
Paul Duffin9d582cc2020-05-16 15:52:12 +01001042// Creates the implementation java library
1043func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
1044 props := struct {
1045 Name *string
1046 Visibility []string
1047 }{
1048 Name: proptools.StringPtr(module.implLibraryModuleName()),
1049 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
1050 }
1051
1052 properties := []interface{}{
1053 &module.properties,
1054 &module.protoProperties,
1055 &module.deviceProperties,
1056 &module.dexpreoptProperties,
1057 &props,
1058 module.sdkComponentPropertiesForChildLibrary(),
1059 }
1060 mctx.CreateModule(LibraryFactory, properties...)
1061}
1062
Jiyong Parkc678ad32018-04-10 13:07:10 +09001063// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001064func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001065 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001066 Name *string
1067 Visibility []string
1068 Srcs []string
1069 Installable *bool
1070 Sdk_version *string
1071 System_modules *string
1072 Patch_module *string
1073 Libs []string
1074 Compile_dex *bool
1075 Java_version *string
1076 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001077 Pdk struct {
1078 Enabled *bool
1079 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001080 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001081 Openjdk9 struct {
1082 Srcs []string
1083 Javacflags []string
1084 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001085 Dist struct {
1086 Targets []string
1087 Dest *string
1088 Dir *string
1089 Tag *string
1090 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001091 }{}
1092
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001093 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +01001094
1095 // If stubs_library_visibility is not set then the created module will use the
1096 // visibility of this module.
1097 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1098 props.Visibility = visibility
1099
Jiyong Parkc678ad32018-04-10 13:07:10 +09001100 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001101 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001102 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001103 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001104 props.System_modules = module.deviceProperties.System_modules
1105 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001106 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001107 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +01001108 // The stub-annotations library contains special versions of the annotations
1109 // with CLASS retention policy, so that they're kept.
1110 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1111 props.Libs = append(props.Libs, "stub-annotations")
1112 }
Jiyong Park82484c02018-04-23 21:41:26 +09001113 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001114 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1115 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hanssoncf4dd4c2020-05-21 09:21:57 +01001116 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1117 // interop with older developer tools that don't support 1.9.
1118 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinc5d954a2020-05-16 18:54:24 +01001119 if module.deviceProperties.Compile_dex != nil {
1120 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001121 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001122
Anton Hansson6bb88102020-03-27 19:43:19 +00001123 // Dist the class jar artifact for sdk builds.
1124 if !Bool(module.sdkLibraryProperties.No_dist) {
1125 props.Dist.Targets = []string{"sdk", "win_sdk"}
1126 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1127 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1128 props.Dist.Tag = proptools.StringPtr(".jar")
1129 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001130
Paul Duffin64e61992020-05-15 10:20:31 +01001131 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001132}
1133
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001134// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001135// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +01001136func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001137 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001138 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001139 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001140 Srcs []string
1141 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001142 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001143 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001144 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001145 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001146 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001147 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +01001148 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001149 Merge_annotations_dirs []string
1150 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001151 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001152 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001153 Current ApiToCheck
1154 Last_released ApiToCheck
1155 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001156
1157 Api_lint struct {
1158 Enabled *bool
1159 New_since *string
1160 Baseline_file *string
1161 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001162 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001163 Aidl struct {
1164 Include_dirs []string
1165 Local_include_dirs []string
1166 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001167 Dist struct {
1168 Targets []string
1169 Dest *string
1170 Dir *string
1171 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001172 }{}
1173
Paul Duffinda364252020-04-28 14:08:32 +01001174 // The stubs source processing uses the same compile time classpath when extracting the
1175 // API from the implementation library as it does when compiling it. i.e. the same
1176 // * sdk version
1177 // * system_modules
1178 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001179
Paul Duffina377e4c2020-04-29 13:30:54 +01001180 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001181
1182 // If stubs_source_visibility is not set then the created module will use the
1183 // visibility of this module.
1184 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1185 props.Visibility = visibility
1186
Paul Duffinc5d954a2020-05-16 18:54:24 +01001187 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1188 props.Sdk_version = module.deviceProperties.Sdk_version
1189 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001190 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001191 // A droiddoc module has only one Libs property and doesn't distinguish between
1192 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001193 props.Libs = module.properties.Libs
1194 props.Libs = append(props.Libs, module.properties.Static_libs...)
1195 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1196 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1197 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001198
Paul Duffin2ce1e812020-05-20 19:35:27 +01001199 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001200 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1201 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1202
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001203 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001204 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001205 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001206 }
1207 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001208 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001209 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1210 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001211 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001212 disabledWarnings := []string{
1213 "MissingPermission",
1214 "BroadcastBehavior",
1215 "HiddenSuperclass",
1216 "DeprecationMismatch",
1217 "UnavailableSymbol",
1218 "SdkConstant",
1219 "HiddenTypeParameter",
1220 "Todo",
1221 "Typo",
1222 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001223 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001224
Paul Duffina377e4c2020-04-29 13:30:54 +01001225 if !createStubSources {
1226 // Stubs are not required.
1227 props.Generate_stubs = proptools.BoolPtr(false)
1228 }
1229
Paul Duffin3c7c3472020-04-07 18:50:10 +01001230 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001231 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001232 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001233 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001234
Paul Duffina377e4c2020-04-29 13:30:54 +01001235 if createApi {
1236 // List of APIs identified from the provided source files are created. They are later
1237 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1238 // last-released (a.k.a numbered) list of API.
1239 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1240 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1241 apiDir := module.getApiDir()
1242 currentApiFileName = path.Join(apiDir, currentApiFileName)
1243 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001244
Paul Duffina377e4c2020-04-29 13:30:54 +01001245 // check against the not-yet-release API
1246 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1247 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001248
Paul Duffina377e4c2020-04-29 13:30:54 +01001249 if !apiScope.unstable {
1250 // check against the latest released API
1251 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1252 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1253 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1254 module.latestRemovedApiFilegroupName(apiScope))
1255 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001256
Paul Duffina377e4c2020-04-29 13:30:54 +01001257 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1258 // Enable api lint.
1259 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1260 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001261
Paul Duffina377e4c2020-04-29 13:30:54 +01001262 // If it exists then pass a lint-baseline.txt through to droidstubs.
1263 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1264 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1265 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1266 if err != nil {
1267 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1268 }
1269 if len(paths) == 1 {
1270 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1271 } else if len(paths) != 0 {
1272 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1273 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001274 }
1275 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001276
Paul Duffina377e4c2020-04-29 13:30:54 +01001277 // Dist the api txt artifact for sdk builds.
1278 if !Bool(module.sdkLibraryProperties.No_dist) {
1279 props.Dist.Targets = []string{"sdk", "win_sdk"}
1280 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1281 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1282 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001283 }
1284
Colin Cross84dfc3d2019-09-25 11:33:01 -07001285 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001286}
1287
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001288func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1289 depTag := mctx.OtherModuleDependencyTag(dep)
1290 if depTag == xmlPermissionsFileTag {
1291 return true
1292 }
1293 return module.Library.DepIsInSameApex(mctx, dep)
1294}
1295
Jiyong Parkc678ad32018-04-10 13:07:10 +09001296// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001297func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001298 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001299 Name *string
1300 Lib_name *string
1301 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001302 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001303 Name: proptools.StringPtr(module.xmlFileName()),
1304 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1305 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001306 }
Jiyong Parke3833882020-02-17 17:28:10 +09001307
Jiyong Parke3833882020-02-17 17:28:10 +09001308 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001309}
1310
Paul Duffin50061512020-01-21 16:31:05 +00001311func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001312 var ver sdkVersion
1313 var kind sdkKind
1314 if s.usePrebuilt(ctx) {
1315 ver = s.version
1316 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001317 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001318 // We don't have prebuilt SDK for the specific sdkVersion.
1319 // Instead of breaking the build, fallback to use "system_current"
1320 ver = sdkVersionCurrent
1321 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001322 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001323
1324 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001325 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001326 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001327 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001328 if ctx.Config().AllowMissingDependencies() {
1329 return android.Paths{android.PathForSource(ctx, jar)}
1330 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001331 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001332 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001333 return nil
1334 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001335 return android.Paths{jarPath.Path()}
1336}
1337
Paul Duffinbf19a972020-05-26 13:21:35 +01001338// Get the apex name for module, "" if it is for platform.
1339func getApexNameForModule(module android.Module) string {
1340 if apex, ok := module.(android.ApexModule); ok {
1341 return apex.ApexName()
1342 }
1343
1344 return ""
1345}
1346
1347// Check to see if the other module is within the same named APEX as this module.
1348//
1349// If either this or the other module are on the platform then this will return
1350// false.
1351func (module *SdkLibrary) withinSameApexAs(other android.Module) bool {
1352 name := module.ApexName()
1353 return name != "" && getApexNameForModule(other) == name
1354}
1355
Paul Duffin47624362020-05-20 12:19:10 +01001356func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001357
Paul Duffin2e7ed652020-05-26 18:13:57 +01001358 // Only provide access to the implementation library if it is actually built.
1359 if module.requiresRuntimeImplementationLibrary() {
1360 // Check any special cases for java_sdk_library.
1361 //
1362 // Only allow access to the implementation library in the following condition:
1363 // * No sdk_version specified on the referencing module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001364 // * The referencing module is in the same apex as this.
1365 if sdkVersion.kind == sdkPrivate || module.withinSameApexAs(ctx.Module()) {
Paul Duffin2e7ed652020-05-26 18:13:57 +01001366 if headerJars {
1367 return module.HeaderJars()
1368 } else {
1369 return module.ImplementationJars()
1370 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001371 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001372 }
Paul Duffin47624362020-05-20 12:19:10 +01001373
Paul Duffina3fb67d2020-05-20 14:20:02 +01001374 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001375}
1376
Sundong Ahn241cd372018-07-13 16:16:44 +09001377// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001378func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1379 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1380}
1381
1382// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001383func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001384 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001385}
1386
Sundong Ahn80a87b32019-05-13 15:02:50 +09001387func (module *SdkLibrary) SetNoDist() {
1388 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1389}
1390
Colin Cross571cccf2019-02-04 11:22:08 -08001391var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1392
Jiyong Park82484c02018-04-23 21:41:26 +09001393func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001394 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001395 return &[]string{}
1396 }).(*[]string)
1397}
1398
Paul Duffin749f98f2019-12-30 17:23:46 +00001399func (module *SdkLibrary) getApiDir() string {
1400 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1401}
1402
Jiyong Parkc678ad32018-04-10 13:07:10 +09001403// For a java_sdk_library module, create internal modules for stubs, docs,
1404// runtime libs and xml file. If requested, the stubs and docs are created twice
1405// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001406func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1407 // If the module has been disabled then don't create any child modules.
1408 if !module.Enabled() {
1409 return
1410 }
1411
Paul Duffinc5d954a2020-05-16 18:54:24 +01001412 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001413 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001414 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001415 }
1416
Paul Duffin37e0b772019-12-30 17:20:10 +00001417 // If this builds against standard libraries (i.e. is not part of the core libraries)
1418 // then assume it provides both system and test apis. Otherwise, assume it does not and
1419 // also assume it does not contribute to the dist build.
1420 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1421 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001422 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001423 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1424
Inseob Kim8098faa2019-03-18 10:19:51 +09001425 missing_current_api := false
1426
Paul Duffin3a254982020-04-28 10:44:03 +01001427 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001428
Paul Duffin749f98f2019-12-30 17:23:46 +00001429 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001430 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001431 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001432 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001433 p := android.ExistentPathForSource(mctx, path)
1434 if !p.Valid() {
1435 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1436 missing_current_api = true
1437 }
1438 }
1439 }
1440
1441 if missing_current_api {
1442 script := "build/soong/scripts/gen-java-current-api-files.sh"
1443 p := android.ExistentPathForSource(mctx, script)
1444
1445 if !p.Valid() {
1446 panic(fmt.Sprintf("script file %s doesn't exist", script))
1447 }
1448
1449 mctx.ModuleErrorf("One or more current api files are missing. "+
1450 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001451 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001452 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001453 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001454 return
1455 }
1456
Paul Duffin3a254982020-04-28 10:44:03 +01001457 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001458 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001459 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001460
1461 // If the args needed to generate the stubs and API are the same then they
1462 // can be generated in a single invocation of metalava, otherwise they will
1463 // need separate invocations.
1464 if scope.createStubsSourceAndApiTogether {
1465 // Use the stubs source name for legacy reasons.
1466 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1467 } else {
1468 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1469
1470 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001471 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001472 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1473 }
1474
Paul Duffind1b3a922020-01-22 11:57:20 +00001475 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001476 }
1477
Paul Duffind11e78e2020-05-15 20:37:11 +01001478 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001479 // Create child module to create an implementation library.
1480 //
1481 // This temporarily creates a second implementation library that can be explicitly
1482 // referenced.
1483 //
1484 // TODO(b/156618935) - update comment once only one implementation library is created.
1485 module.createImplLibrary(mctx)
1486
Paul Duffind11e78e2020-05-15 20:37:11 +01001487 // Only create an XML permissions file that declares the library as being usable
1488 // as a shared library if required.
1489 if module.sharedLibrary() {
1490 module.createXmlFile(mctx)
1491 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001492
1493 // record java_sdk_library modules so that they are exported to make
1494 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1495 javaSdkLibrariesLock.Lock()
1496 defer javaSdkLibrariesLock.Unlock()
1497 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1498 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001499}
1500
1501func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001502 module.AddProperties(
1503 &module.sdkLibraryProperties,
Paul Duffinc5d954a2020-05-16 18:54:24 +01001504 &module.properties,
1505 &module.dexpreoptProperties,
1506 &module.deviceProperties,
1507 &module.protoProperties,
Sundong Ahn054b19a2018-10-19 13:46:09 +09001508 )
1509
Paul Duffin64e61992020-05-15 10:20:31 +01001510 module.initSdkLibraryComponent(&module.ModuleBase)
1511
Paul Duffinc5d954a2020-05-16 18:54:24 +01001512 module.properties.Installable = proptools.BoolPtr(true)
1513 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001514}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001515
Paul Duffind11e78e2020-05-15 20:37:11 +01001516func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1517 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1518}
1519
Paul Duffin1a724e62020-05-08 13:44:43 +01001520// Defines how to name the individual component modules the sdk library creates.
1521type sdkLibraryComponentNamingScheme interface {
1522 stubsLibraryModuleName(scope *apiScope, baseName string) string
1523
1524 stubsSourceModuleName(scope *apiScope, baseName string) string
1525
1526 apiModuleName(scope *apiScope, baseName string) string
1527}
1528
1529type defaultNamingScheme struct {
1530}
1531
1532func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1533 return scope.stubsLibraryModuleName(baseName)
1534}
1535
1536func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1537 return scope.stubsSourceModuleName(baseName)
1538}
1539
1540func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1541 return scope.apiModuleName(baseName)
1542}
1543
1544var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1545
Paul Duffindef8a892020-05-08 15:36:30 +01001546type frameworkModulesNamingScheme struct {
1547}
1548
1549func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1550 suffix := scope.name
1551 if scope == apiScopeModuleLib {
1552 suffix = "module_libs_"
1553 }
1554 return suffix
1555}
1556
1557func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1558 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1559}
1560
1561func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1562 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1563}
1564
1565func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1566 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1567}
1568
1569var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1570
Anton Hansson0bd88d02020-05-25 12:20:51 +01001571func moduleStubLinkType(name string) (stub bool, ret linkType) {
1572 // This suffix-based approach is fragile and could potentially mis-trigger.
1573 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1574 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1575 return true, javaSdk
1576 }
1577 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1578 return true, javaSystem
1579 }
1580 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1581 return true, javaModule
1582 }
1583 if strings.HasSuffix(name, ".stubs.test") {
1584 return true, javaSystem
1585 }
1586 return false, javaPlatform
1587}
1588
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001589// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1590// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1591// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1592// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1593// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001594func SdkLibraryFactory() android.Module {
1595 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001596
1597 // Initialize information common between source and prebuilt.
1598 module.initCommon(&module.ModuleBase)
1599
Inseob Kimc0907f12019-02-08 21:00:45 +09001600 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001601 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001602 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001603
1604 // Initialize the map from scope to scope specific properties.
1605 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1606 for _, scope := range allApiScopes {
1607 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1608 }
1609 module.scopeToProperties = scopeToProperties
1610
Paul Duffin344c4ee2020-04-29 23:35:13 +01001611 // Add the properties containing visibility rules so that they are checked.
Paul Duffin9d582cc2020-05-16 15:52:12 +01001612 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001613 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1614 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1615
Paul Duffin1a724e62020-05-08 13:44:43 +01001616 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001617 // If no implementation is required then it cannot be used as a shared library
1618 // either.
1619 if !module.requiresRuntimeImplementationLibrary() {
1620 // If shared_library has been explicitly set to true then it is incompatible
1621 // with api_only: true.
1622 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1623 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1624 }
1625 // Set shared_library: false.
1626 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1627 }
1628
Paul Duffin1a724e62020-05-08 13:44:43 +01001629 if module.initCommonAfterDefaultsApplied(ctx) {
1630 module.CreateInternalModules(ctx)
1631 }
1632 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001633 return module
1634}
Colin Cross79c7c262019-04-17 11:11:46 -07001635
1636//
1637// SDK library prebuilts
1638//
1639
Paul Duffin56d44902020-01-31 13:36:25 +00001640// Properties associated with each api scope.
1641type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001642 Jars []string `android:"path"`
1643
1644 Sdk_version *string
1645
Colin Cross79c7c262019-04-17 11:11:46 -07001646 // List of shared java libs that this module has dependencies to
1647 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001648
Paul Duffin5fb82132020-04-29 20:45:27 +01001649 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001650 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001651
1652 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001653 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001654
1655 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001656 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001657}
1658
Paul Duffin56d44902020-01-31 13:36:25 +00001659type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001660 // List of shared java libs, common to all scopes, that this module has
1661 // dependencies to
1662 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001663}
1664
Colin Cross79c7c262019-04-17 11:11:46 -07001665type sdkLibraryImport struct {
1666 android.ModuleBase
1667 android.DefaultableModuleBase
1668 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001669 android.ApexModuleBase
1670 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001671
1672 properties sdkLibraryImportProperties
1673
Paul Duffin6a2bd112020-04-07 19:27:04 +01001674 // Map from api scope to the scope specific property structure.
1675 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1676
Paul Duffin56d44902020-01-31 13:36:25 +00001677 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001678}
1679
1680var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1681
Paul Duffin6a2bd112020-04-07 19:27:04 +01001682// The type of a structure that contains a field of type sdkLibraryScopeProperties
1683// for each apiscope in allApiScopes, e.g. something like:
1684// struct {
1685// Public sdkLibraryScopeProperties
1686// System sdkLibraryScopeProperties
1687// ...
1688// }
1689var allScopeStructType = createAllScopePropertiesStructType()
1690
1691// Dynamically create a structure type for each apiscope in allApiScopes.
1692func createAllScopePropertiesStructType() reflect.Type {
1693 var fields []reflect.StructField
1694 for _, apiScope := range allApiScopes {
1695 field := reflect.StructField{
1696 Name: apiScope.fieldName,
1697 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1698 }
1699 fields = append(fields, field)
1700 }
1701
1702 return reflect.StructOf(fields)
1703}
1704
1705// Create an instance of the scope specific structure type and return a map
1706// from apiscope to a pointer to each scope specific field.
1707func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1708 allScopePropertiesPtr := reflect.New(allScopeStructType)
1709 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1710 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1711
1712 for _, apiScope := range allApiScopes {
1713 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1714 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1715 }
1716
1717 return allScopePropertiesPtr.Interface(), scopeProperties
1718}
1719
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001720// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001721func sdkLibraryImportFactory() android.Module {
1722 module := &sdkLibraryImport{}
1723
Paul Duffin6a2bd112020-04-07 19:27:04 +01001724 allScopeProperties, scopeToProperties := createPropertiesInstance()
1725 module.scopeProperties = scopeToProperties
1726 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001727
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001728 // Initialize information common between source and prebuilt.
1729 module.initCommon(&module.ModuleBase)
1730
Paul Duffin0bdcb272020-02-06 15:24:57 +00001731 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001732 android.InitApexModule(module)
1733 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001734 InitJavaModule(module, android.HostAndDeviceSupported)
1735
Paul Duffin1a724e62020-05-08 13:44:43 +01001736 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1737 if module.initCommonAfterDefaultsApplied(mctx) {
1738 module.createInternalModules(mctx)
1739 }
1740 })
Colin Cross79c7c262019-04-17 11:11:46 -07001741 return module
1742}
1743
1744func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1745 return &module.prebuilt
1746}
1747
1748func (module *sdkLibraryImport) Name() string {
1749 return module.prebuilt.Name(module.ModuleBase.Name())
1750}
1751
Paul Duffinbf735aa2020-05-08 15:01:19 +01001752func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001753
Paul Duffin50061512020-01-21 16:31:05 +00001754 // If the build is configured to use prebuilts then force this to be preferred.
1755 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1756 module.prebuilt.ForcePrefer()
1757 }
1758
Paul Duffin6a2bd112020-04-07 19:27:04 +01001759 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001760 if len(scopeProperties.Jars) == 0 {
1761 continue
1762 }
1763
Paul Duffinf6155722020-04-09 00:07:11 +01001764 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001765
Paul Duffin533f9c72020-05-20 16:18:00 +01001766 if len(scopeProperties.Stub_srcs) > 0 {
1767 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1768 }
Paul Duffin56d44902020-01-31 13:36:25 +00001769 }
Colin Cross79c7c262019-04-17 11:11:46 -07001770
1771 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1772 javaSdkLibrariesLock.Lock()
1773 defer javaSdkLibrariesLock.Unlock()
1774 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1775}
1776
Paul Duffinbf735aa2020-05-08 15:01:19 +01001777func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001778 // Creates a java import for the jar with ".stubs" suffix
1779 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001780 Name *string
1781 Sdk_version *string
1782 Libs []string
1783 Jars []string
1784 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001785 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001786 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001787 props.Sdk_version = scopeProperties.Sdk_version
1788 // Prepend any of the libs from the legacy public properties to the libs for each of the
1789 // scopes to avoid having to duplicate them in each scope.
1790 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1791 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001792
Paul Duffindd89a282020-05-13 16:08:09 +01001793 // The imports are preferred if the java_sdk_library_import is preferred.
1794 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001795
1796 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001797}
1798
Paul Duffinbf735aa2020-05-08 15:01:19 +01001799func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001800 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001801 Name *string
1802 Srcs []string
1803 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001804 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001805 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001806 props.Srcs = scopeProperties.Stub_srcs
1807 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001808
1809 // The stubs source is preferred if the java_sdk_library_import is preferred.
1810 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001811}
1812
Colin Cross79c7c262019-04-17 11:11:46 -07001813func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001814 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001815 if len(scopeProperties.Jars) == 0 {
1816 continue
1817 }
1818
1819 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001820 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001821
1822 if len(scopeProperties.Stub_srcs) > 0 {
1823 // Add dependencies to the prebuilt stubs source library
1824 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1825 }
Paul Duffin56d44902020-01-31 13:36:25 +00001826 }
Colin Cross79c7c262019-04-17 11:11:46 -07001827}
1828
Paul Duffin46fdda82020-05-14 15:39:10 +01001829func (module *sdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
1830 return module.commonOutputFiles(tag)
1831}
1832
Colin Cross79c7c262019-04-17 11:11:46 -07001833func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001834 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001835 ctx.VisitDirectDeps(func(to android.Module) {
1836 tag := ctx.OtherModuleDependencyTag(to)
1837
Paul Duffin533f9c72020-05-20 16:18:00 +01001838 // Extract information from any of the scope specific dependencies.
1839 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1840 apiScope := scopeTag.apiScope
1841 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1842
1843 // Extract information from the dependency. The exact information extracted
1844 // is determined by the nature of the dependency which is determined by the tag.
1845 scopeTag.extractDepInfo(ctx, to, scopePaths)
Colin Cross79c7c262019-04-17 11:11:46 -07001846 }
1847 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001848
1849 // Populate the scope paths with information from the properties.
1850 for apiScope, scopeProperties := range module.scopeProperties {
1851 if len(scopeProperties.Jars) == 0 {
1852 continue
1853 }
1854
1855 paths := module.getScopePathsCreateIfNeeded(apiScope)
1856 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1857 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1858 }
Colin Cross79c7c262019-04-17 11:11:46 -07001859}
1860
Paul Duffin47624362020-05-20 12:19:10 +01001861func (module *sdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffina3fb67d2020-05-20 14:20:02 +01001862 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001863}
1864
Colin Cross79c7c262019-04-17 11:11:46 -07001865// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001866func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001867 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001868 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001869}
1870
1871// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001872func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001873 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001874 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001875}
Jiyong Parke3833882020-02-17 17:28:10 +09001876
1877//
1878// java_sdk_library_xml
1879//
1880type sdkLibraryXml struct {
1881 android.ModuleBase
1882 android.DefaultableModuleBase
1883 android.ApexModuleBase
1884
1885 properties sdkLibraryXmlProperties
1886
1887 outputFilePath android.OutputPath
1888 installDirPath android.InstallPath
1889}
1890
1891type sdkLibraryXmlProperties struct {
1892 // canonical name of the lib
1893 Lib_name *string
1894}
1895
1896// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1897// Not to be used directly by users. java_sdk_library internally uses this.
1898func sdkLibraryXmlFactory() android.Module {
1899 module := &sdkLibraryXml{}
1900
1901 module.AddProperties(&module.properties)
1902
1903 android.InitApexModule(module)
1904 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1905
1906 return module
1907}
1908
1909// from android.PrebuiltEtcModule
1910func (module *sdkLibraryXml) SubDir() string {
1911 return "permissions"
1912}
1913
1914// from android.PrebuiltEtcModule
1915func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1916 return module.outputFilePath
1917}
1918
1919// from android.ApexModule
1920func (module *sdkLibraryXml) AvailableFor(what string) bool {
1921 return true
1922}
1923
1924func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1925 // do nothing
1926}
1927
1928// File path to the runtime implementation library
1929func (module *sdkLibraryXml) implPath() string {
1930 implName := proptools.String(module.properties.Lib_name)
1931 if apexName := module.ApexName(); apexName != "" {
1932 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1933 // In most cases, this works fine. But when apex_name is set or override_apex is used
1934 // this can be wrong.
1935 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1936 }
1937 partition := "system"
1938 if module.SocSpecific() {
1939 partition = "vendor"
1940 } else if module.DeviceSpecific() {
1941 partition = "odm"
1942 } else if module.ProductSpecific() {
1943 partition = "product"
1944 } else if module.SystemExtSpecific() {
1945 partition = "system_ext"
1946 }
1947 return "/" + partition + "/framework/" + implName + ".jar"
1948}
1949
1950func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1951 libName := proptools.String(module.properties.Lib_name)
1952 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1953
1954 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1955 rule := android.NewRuleBuilder()
1956 rule.Command().
1957 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1958 Output(module.outputFilePath)
1959
1960 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1961
1962 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1963}
1964
1965func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1966 if !module.IsForPlatform() {
1967 return []android.AndroidMkEntries{android.AndroidMkEntries{
1968 Disabled: true,
1969 }}
1970 }
1971
1972 return []android.AndroidMkEntries{android.AndroidMkEntries{
1973 Class: "ETC",
1974 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1975 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1976 func(entries *android.AndroidMkEntries) {
1977 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1978 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1979 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1980 },
1981 },
1982 }}
1983}
Paul Duffin61871622020-02-10 13:37:10 +00001984
1985type sdkLibrarySdkMemberType struct {
1986 android.SdkMemberTypeBase
1987}
1988
1989func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1990 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1991}
1992
1993func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1994 _, ok := module.(*SdkLibrary)
1995 return ok
1996}
1997
1998func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1999 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2000}
2001
2002func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2003 return &sdkLibrarySdkMemberProperties{}
2004}
2005
2006type sdkLibrarySdkMemberProperties struct {
2007 android.SdkMemberPropertiesBase
2008
2009 // Scope to per scope properties.
2010 Scopes map[*apiScope]scopeProperties
2011
2012 // Additional libraries that the exported stubs libraries depend upon.
2013 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01002014
2015 // The Java stubs source files.
2016 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01002017
2018 // The naming scheme.
2019 Naming_scheme *string
Paul Duffin61871622020-02-10 13:37:10 +00002020}
2021
2022type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01002023 Jars android.Paths
2024 StubsSrcJar android.Path
2025 CurrentApiFile android.Path
2026 RemovedApiFile android.Path
2027 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00002028}
2029
2030func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2031 sdk := variant.(*SdkLibrary)
2032
2033 s.Scopes = make(map[*apiScope]scopeProperties)
2034 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01002035 paths := sdk.findScopePaths(apiScope)
2036 if paths == nil {
2037 continue
2038 }
2039
Paul Duffin61871622020-02-10 13:37:10 +00002040 jars := paths.stubsImplPath
2041 if len(jars) > 0 {
2042 properties := scopeProperties{}
2043 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01002044 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01002045 properties.StubsSrcJar = paths.stubsSrcJar.Path()
2046 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2047 properties.RemovedApiFile = paths.removedApiFilePath.Path()
Paul Duffin61871622020-02-10 13:37:10 +00002048 s.Scopes[apiScope] = properties
2049 }
2050 }
2051
2052 s.Libs = sdk.properties.Libs
Paul Duffind11e78e2020-05-15 20:37:11 +01002053 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffin61871622020-02-10 13:37:10 +00002054}
2055
2056func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01002057 if s.Naming_scheme != nil {
2058 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2059 }
2060
Paul Duffin61871622020-02-10 13:37:10 +00002061 for _, apiScope := range allApiScopes {
2062 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01002063 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00002064
Paul Duffinf488ef22020-04-09 00:10:17 +01002065 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2066
Paul Duffin61871622020-02-10 13:37:10 +00002067 var jars []string
2068 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01002069 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00002070 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2071 jars = append(jars, dest)
2072 }
2073 scopeSet.AddProperty("jars", jars)
2074
Paul Duffinf488ef22020-04-09 00:10:17 +01002075 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2076 // the source files are also unpacked.
2077 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2078 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2079 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2080
Paul Duffin75dcc802020-04-09 01:08:11 +01002081 if properties.CurrentApiFile != nil {
2082 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2083 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2084 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2085 }
2086
2087 if properties.RemovedApiFile != nil {
2088 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
2089 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
2090 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2091 }
2092
Paul Duffin61871622020-02-10 13:37:10 +00002093 if properties.SdkVersion != "" {
2094 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2095 }
2096 }
2097 }
2098
2099 if len(s.Libs) > 0 {
2100 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2101 }
2102}