blob: 70bfdfb3f8c60c0ee07e9beb3e4ad5525659d48e [file] [log] [blame]
Paul Duffinc6bb7cf2021-04-08 17:49:27 +01001// Copyright (C) 2021 The Android Open Source Project
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 (
Paul Duffin7487a7a2021-05-19 09:36:09 +010018 "fmt"
Paul Duffindfa10832021-05-13 17:31:51 +010019 "strings"
20
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010021 "android/soong/android"
Paul Duffin9b381ef2021-04-08 23:01:37 +010022 "github.com/google/blueprint"
Paul Duffin62370922021-05-23 16:55:37 +010023 "github.com/google/blueprint/proptools"
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010024)
25
26// Contains support for processing hiddenAPI in a modular fashion.
27
Paul Duffin31fad802021-06-18 18:14:25 +010028// HiddenAPIScope encapsulates all the information that the hidden API processing needs about API
29// scopes, i.e. what is called android.SdkKind and apiScope. It does not just use those as they do
30// not provide the information needed by hidden API processing.
31type HiddenAPIScope struct {
32 // The name of the scope, used for debug purposes.
33 name string
34
35 // The corresponding android.SdkKind, used for retrieving paths from java_sdk_library* modules.
36 sdkKind android.SdkKind
37
38 // The option needed to passed to "hiddenapi list".
39 hiddenAPIListOption string
Paul Duffin5cca7c42021-05-26 10:16:01 +010040
41 // The name sof the source stub library modules that contain the API provided by the platform,
42 // i.e. by modules that are not in an APEX.
43 nonUpdatableSourceModule string
44
45 // The names of the prebuilt stub library modules that contain the API provided by the platform,
46 // i.e. by modules that are not in an APEX.
47 nonUpdatablePrebuiltModule string
Paul Duffin31fad802021-06-18 18:14:25 +010048}
49
50// initHiddenAPIScope initializes the scope.
51func initHiddenAPIScope(apiScope *HiddenAPIScope) *HiddenAPIScope {
Paul Duffin5cca7c42021-05-26 10:16:01 +010052 sdkKind := apiScope.sdkKind
53 // The platform does not provide a core platform API.
54 if sdkKind != android.SdkCorePlatform {
55 kindAsString := sdkKind.String()
56 var insert string
57 if sdkKind == android.SdkPublic {
58 insert = ""
59 } else {
60 insert = "." + strings.ReplaceAll(kindAsString, "-", "_")
61 }
62
63 nonUpdatableModule := "android-non-updatable"
64
65 // Construct the name of the android-non-updatable source module for this scope.
66 apiScope.nonUpdatableSourceModule = fmt.Sprintf("%s.stubs%s", nonUpdatableModule, insert)
67
68 prebuiltModuleName := func(name string, kind string) string {
69 return fmt.Sprintf("sdk_%s_current_%s", kind, name)
70 }
71
72 // Construct the name of the android-non-updatable prebuilt module for this scope.
73 apiScope.nonUpdatablePrebuiltModule = prebuiltModuleName(nonUpdatableModule, kindAsString)
74 }
75
Paul Duffin31fad802021-06-18 18:14:25 +010076 return apiScope
77}
78
Paul Duffin5cca7c42021-05-26 10:16:01 +010079// android-non-updatable takes the name of a module and returns a possibly scope specific name of
80// the module.
81func (l *HiddenAPIScope) scopeSpecificStubModule(ctx android.BaseModuleContext, name string) string {
82 // The android-non-updatable is not a java_sdk_library but there are separate stub libraries for
83 // each scope.
84 // TODO(b/192067200): Remove special handling of android-non-updatable.
85 if name == "android-non-updatable" {
86 if ctx.Config().AlwaysUsePrebuiltSdks() {
87 return l.nonUpdatablePrebuiltModule
88 } else {
89 return l.nonUpdatableSourceModule
90 }
91 } else {
92 // Assume that the module is either a java_sdk_library (or equivalent) and so will provide
93 // separate stub jars for each scope or is a java_library (or equivalent) in which case it will
94 // have the same stub jar for each scope.
95 return name
96 }
97}
98
Paul Duffin31fad802021-06-18 18:14:25 +010099func (l *HiddenAPIScope) String() string {
100 return fmt.Sprintf("HiddenAPIScope{%s}", l.name)
101}
102
103var (
104 PublicHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{
105 name: "public",
106 sdkKind: android.SdkPublic,
107 hiddenAPIListOption: "--public-stub-classpath",
108 })
109 SystemHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{
110 name: "system",
111 sdkKind: android.SdkSystem,
112 hiddenAPIListOption: "--system-stub-classpath",
113 })
114 TestHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{
115 name: "test",
116 sdkKind: android.SdkTest,
117 hiddenAPIListOption: "--test-stub-classpath",
118 })
Paul Duffinb51db2e2021-06-21 14:08:08 +0100119 ModuleLibHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{
120 name: "module-lib",
121 sdkKind: android.SdkModule,
122 })
Paul Duffin31fad802021-06-18 18:14:25 +0100123 CorePlatformHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{
124 name: "core-platform",
125 sdkKind: android.SdkCorePlatform,
126 hiddenAPIListOption: "--core-platform-stub-classpath",
127 })
128
129 // hiddenAPIRelevantSdkKinds lists all the android.SdkKind instances that are needed by the hidden
130 // API processing.
131 //
132 // These are roughly in order from narrowest API surface to widest. Widest means the API stubs
133 // with the biggest API surface, e.g. test is wider than system is wider than public.
134 //
Paul Duffinb51db2e2021-06-21 14:08:08 +0100135 // Core platform is considered wider than system/module-lib because those modules that provide
136 // core platform APIs either do not have any system/module-lib APIs at all, or if they do it is
137 // because the core platform API is being converted to system/module-lib APIs. In either case the
138 // system/module-lib APIs are subsets of the core platform API.
Paul Duffin31fad802021-06-18 18:14:25 +0100139 //
140 // This is not strictly in order from narrowest to widest as the Test API is wider than system but
Paul Duffinb51db2e2021-06-21 14:08:08 +0100141 // is neither wider or narrower than the module-lib or core platform APIs. However, this works
142 // well enough at the moment.
Paul Duffin31fad802021-06-18 18:14:25 +0100143 // TODO(b/191644675): Correctly reflect the sub/superset relationships between APIs.
144 hiddenAPIScopes = []*HiddenAPIScope{
145 PublicHiddenAPIScope,
146 SystemHiddenAPIScope,
147 TestHiddenAPIScope,
Paul Duffinb51db2e2021-06-21 14:08:08 +0100148 ModuleLibHiddenAPIScope,
Paul Duffin31fad802021-06-18 18:14:25 +0100149 CorePlatformHiddenAPIScope,
150 }
151
152 // The HiddenAPIScope instances that are supported by a java_sdk_library.
153 //
154 // CorePlatformHiddenAPIScope is not used as the java_sdk_library does not have special support
155 // for core_platform API, instead it is implemented as a customized form of PublicHiddenAPIScope.
156 hiddenAPISdkLibrarySupportedScopes = []*HiddenAPIScope{
157 PublicHiddenAPIScope,
158 SystemHiddenAPIScope,
159 TestHiddenAPIScope,
Paul Duffinb51db2e2021-06-21 14:08:08 +0100160 ModuleLibHiddenAPIScope,
Paul Duffin31fad802021-06-18 18:14:25 +0100161 }
162
163 // The HiddenAPIScope instances that are supported by the `hiddenapi list`.
164 hiddenAPIFlagScopes = []*HiddenAPIScope{
165 PublicHiddenAPIScope,
166 SystemHiddenAPIScope,
167 TestHiddenAPIScope,
168 CorePlatformHiddenAPIScope,
169 }
170)
171
Paul Duffin74431d52021-04-21 14:10:42 +0100172type hiddenAPIStubsDependencyTag struct {
173 blueprint.BaseDependencyTag
Paul Duffin31fad802021-06-18 18:14:25 +0100174
175 // The api scope for which this dependency was added.
176 apiScope *HiddenAPIScope
Paul Duffin5cca7c42021-05-26 10:16:01 +0100177
178 // Indicates that the dependency is not for an API provided by the current bootclasspath fragment
179 // but is an additional API provided by a module that is not part of the current bootclasspath
180 // fragment.
181 fromAdditionalDependency bool
Paul Duffin74431d52021-04-21 14:10:42 +0100182}
183
184func (b hiddenAPIStubsDependencyTag) ExcludeFromApexContents() {
185}
186
187func (b hiddenAPIStubsDependencyTag) ReplaceSourceWithPrebuilt() bool {
188 return false
189}
190
Paul Duffin976b0e52021-04-27 23:20:26 +0100191func (b hiddenAPIStubsDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType {
Paul Duffin5cca7c42021-05-26 10:16:01 +0100192 // Do not add additional dependencies to the sdk.
193 if b.fromAdditionalDependency {
194 return nil
195 }
196
Paul Duffin976b0e52021-04-27 23:20:26 +0100197 // If the module is a java_sdk_library then treat it as if it was specific in the java_sdk_libs
198 // property, otherwise treat if it was specified in the java_header_libs property.
199 if javaSdkLibrarySdkMemberType.IsInstance(child) {
200 return javaSdkLibrarySdkMemberType
201 }
202
203 return javaHeaderLibsSdkMemberType
204}
205
206func (b hiddenAPIStubsDependencyTag) ExportMember() bool {
207 // Export the module added via this dependency tag from the sdk.
208 return true
209}
210
Paul Duffin74431d52021-04-21 14:10:42 +0100211// Avoid having to make stubs content explicitly visible to dependent modules.
212//
213// This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules
214// with proper dependencies.
215// TODO(b/177892522): Remove this and add needed visibility.
216func (b hiddenAPIStubsDependencyTag) ExcludeFromVisibilityEnforcement() {
217}
218
219var _ android.ExcludeFromVisibilityEnforcementTag = hiddenAPIStubsDependencyTag{}
220var _ android.ReplaceSourceWithPrebuilt = hiddenAPIStubsDependencyTag{}
221var _ android.ExcludeFromApexContentsTag = hiddenAPIStubsDependencyTag{}
Paul Duffin976b0e52021-04-27 23:20:26 +0100222var _ android.SdkMemberTypeDependencyTag = hiddenAPIStubsDependencyTag{}
Paul Duffin74431d52021-04-21 14:10:42 +0100223
Paul Duffin74431d52021-04-21 14:10:42 +0100224// hiddenAPIComputeMonolithicStubLibModules computes the set of module names that provide stubs
225// needed to produce the hidden API monolithic stub flags file.
Paul Duffin31fad802021-06-18 18:14:25 +0100226func hiddenAPIComputeMonolithicStubLibModules(config android.Config) map[*HiddenAPIScope][]string {
Paul Duffin74431d52021-04-21 14:10:42 +0100227 var publicStubModules []string
228 var systemStubModules []string
229 var testStubModules []string
230 var corePlatformStubModules []string
231
232 if config.AlwaysUsePrebuiltSdks() {
233 // Build configuration mandates using prebuilt stub modules
234 publicStubModules = append(publicStubModules, "sdk_public_current_android")
235 systemStubModules = append(systemStubModules, "sdk_system_current_android")
236 testStubModules = append(testStubModules, "sdk_test_current_android")
237 } else {
238 // Use stub modules built from source
239 publicStubModules = append(publicStubModules, "android_stubs_current")
240 systemStubModules = append(systemStubModules, "android_system_stubs_current")
241 testStubModules = append(testStubModules, "android_test_stubs_current")
242 }
243 // We do not have prebuilts of the core platform api yet
244 corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs")
245
246 // Allow products to define their own stubs for custom product jars that apps can use.
247 publicStubModules = append(publicStubModules, config.ProductHiddenAPIStubs()...)
248 systemStubModules = append(systemStubModules, config.ProductHiddenAPIStubsSystem()...)
249 testStubModules = append(testStubModules, config.ProductHiddenAPIStubsTest()...)
250 if config.IsEnvTrue("EMMA_INSTRUMENT") {
Paul Duffin098c8782021-05-14 10:45:25 +0100251 // Add jacoco-stubs to public, system and test. It doesn't make any real difference as public
252 // allows everyone access but it is needed to ensure consistent flags between the
253 // bootclasspath fragment generated flags and the platform_bootclasspath generated flags.
Paul Duffin74431d52021-04-21 14:10:42 +0100254 publicStubModules = append(publicStubModules, "jacoco-stubs")
Paul Duffin098c8782021-05-14 10:45:25 +0100255 systemStubModules = append(systemStubModules, "jacoco-stubs")
256 testStubModules = append(testStubModules, "jacoco-stubs")
Paul Duffin74431d52021-04-21 14:10:42 +0100257 }
258
Paul Duffin31fad802021-06-18 18:14:25 +0100259 m := map[*HiddenAPIScope][]string{}
260 m[PublicHiddenAPIScope] = publicStubModules
261 m[SystemHiddenAPIScope] = systemStubModules
262 m[TestHiddenAPIScope] = testStubModules
263 m[CorePlatformHiddenAPIScope] = corePlatformStubModules
Paul Duffin74431d52021-04-21 14:10:42 +0100264 return m
265}
266
267// hiddenAPIAddStubLibDependencies adds dependencies onto the modules specified in
Paul Duffin31fad802021-06-18 18:14:25 +0100268// apiScopeToStubLibModules. It adds them in a well known order and uses a HiddenAPIScope specific
269// tag to identify the source of the dependency.
270func hiddenAPIAddStubLibDependencies(ctx android.BottomUpMutatorContext, apiScopeToStubLibModules map[*HiddenAPIScope][]string) {
Paul Duffin74431d52021-04-21 14:10:42 +0100271 module := ctx.Module()
Paul Duffin31fad802021-06-18 18:14:25 +0100272 for _, apiScope := range hiddenAPIScopes {
273 modules := apiScopeToStubLibModules[apiScope]
274 ctx.AddDependency(module, hiddenAPIStubsDependencyTag{apiScope: apiScope}, modules...)
Paul Duffin74431d52021-04-21 14:10:42 +0100275 }
276}
277
Paul Duffin74431d52021-04-21 14:10:42 +0100278// hiddenAPIRetrieveDexJarBuildPath retrieves the DexJarBuildPath from the specified module, if
279// available, or reports an error.
Paul Duffin10931582021-04-25 10:13:54 +0100280func hiddenAPIRetrieveDexJarBuildPath(ctx android.ModuleContext, module android.Module, kind android.SdkKind) android.Path {
281 var dexJar android.Path
282 if sdkLibrary, ok := module.(SdkLibraryDependency); ok {
283 dexJar = sdkLibrary.SdkApiStubDexJar(ctx, kind)
284 } else if j, ok := module.(UsesLibraryDependency); ok {
285 dexJar = j.DexJarBuildPath()
Paul Duffin74431d52021-04-21 14:10:42 +0100286 } else {
287 ctx.ModuleErrorf("dependency %s of module type %s does not support providing a dex jar", module, ctx.OtherModuleType(module))
Paul Duffin10931582021-04-25 10:13:54 +0100288 return nil
Paul Duffin74431d52021-04-21 14:10:42 +0100289 }
Paul Duffin10931582021-04-25 10:13:54 +0100290
291 if dexJar == nil {
292 ctx.ModuleErrorf("dependency %s does not provide a dex jar, consider setting compile_dex: true", module)
293 }
294 return dexJar
Paul Duffin74431d52021-04-21 14:10:42 +0100295}
296
Paul Duffin4539a372021-06-23 23:20:43 +0100297// buildRuleToGenerateHiddenAPIStubFlagsFile creates a rule to create a hidden API stub flags file.
Paul Duffin74431d52021-04-21 14:10:42 +0100298//
299// The rule is initialized but not built so that the caller can modify it and select an appropriate
300// name.
Paul Duffin2e880972021-06-23 23:29:09 +0100301func buildRuleToGenerateHiddenAPIStubFlagsFile(ctx android.BuilderContext, name, desc string, outputPath android.WritablePath, bootDexJars android.Paths, input HiddenAPIFlagInput, moduleStubFlagsPaths android.Paths) {
Paul Duffin74431d52021-04-21 14:10:42 +0100302 // Singleton rule which applies hiddenapi on all boot class path dex files.
303 rule := android.NewRuleBuilder(pctx, ctx)
304
305 tempPath := tempPathForRestat(ctx, outputPath)
306
Paul Duffinf1b358c2021-05-17 07:38:47 +0100307 // Find the widest API stubs provided by the fragments on which this depends, if any.
Paul Duffin5cca7c42021-05-26 10:16:01 +0100308 dependencyStubDexJars := input.DependencyStubDexJarsByScope.stubDexJarsForWidestAPIScope()
309
310 // Add widest API stubs from the additional dependencies of this, if any.
311 dependencyStubDexJars = append(dependencyStubDexJars, input.AdditionalStubDexJarsByScope.stubDexJarsForWidestAPIScope()...)
Paul Duffinf1b358c2021-05-17 07:38:47 +0100312
Paul Duffin74431d52021-04-21 14:10:42 +0100313 command := rule.Command().
314 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
315 Text("list").
Paul Duffinf1b358c2021-05-17 07:38:47 +0100316 FlagForEachInput("--dependency-stub-dex=", dependencyStubDexJars).
Paul Duffin74431d52021-04-21 14:10:42 +0100317 FlagForEachInput("--boot-dex=", bootDexJars)
318
Paul Duffin156b5d32021-06-24 23:06:52 +0100319 // If no module stub flags paths are provided then this must be being called for a
320 // bootclasspath_fragment and not the whole platform_bootclasspath.
321 if moduleStubFlagsPaths == nil {
322 // This is being run on a fragment of the bootclasspath.
323 command.Flag("--fragment")
324 }
325
Paul Duffin31fad802021-06-18 18:14:25 +0100326 // Iterate over the api scopes in a fixed order.
327 for _, apiScope := range hiddenAPIFlagScopes {
328 // Merge in the stub dex jar paths for this api scope from the fragments on which it depends.
329 // They will be needed to resolve dependencies from this fragment's stubs to classes in the
330 // other fragment's APIs.
331 var paths android.Paths
332 paths = append(paths, input.DependencyStubDexJarsByScope[apiScope]...)
Paul Duffin5cca7c42021-05-26 10:16:01 +0100333 paths = append(paths, input.AdditionalStubDexJarsByScope[apiScope]...)
Paul Duffin31fad802021-06-18 18:14:25 +0100334 paths = append(paths, input.StubDexJarsByScope[apiScope]...)
Paul Duffin74431d52021-04-21 14:10:42 +0100335 if len(paths) > 0 {
Paul Duffin31fad802021-06-18 18:14:25 +0100336 option := apiScope.hiddenAPIListOption
337 command.FlagWithInputList(option+"=", paths, ":")
Paul Duffin74431d52021-04-21 14:10:42 +0100338 }
339 }
340
341 // Add the output path.
342 command.FlagWithOutput("--out-api-flags=", tempPath)
343
Paul Duffin2e880972021-06-23 23:29:09 +0100344 // If there are stub flag files that have been generated by fragments on which this depends then
345 // use them to validate the stub flag file generated by the rules created by this method.
346 if len(moduleStubFlagsPaths) > 0 {
347 validFile := buildRuleValidateOverlappingCsvFiles(ctx, name, desc, outputPath, moduleStubFlagsPaths)
348
349 // Add the file that indicates that the file generated by this is valid.
350 //
351 // This will cause the validation rule above to be run any time that the output of this rule
352 // changes but the validation will run in parallel with other rules that depend on this file.
353 command.Validation(validFile)
354 }
355
Paul Duffin74431d52021-04-21 14:10:42 +0100356 commitChangeForRestat(rule, tempPath, outputPath)
Paul Duffin4539a372021-06-23 23:20:43 +0100357
358 rule.Build(name, desc)
Paul Duffin74431d52021-04-21 14:10:42 +0100359}
360
Paul Duffin46169772021-04-14 15:01:56 +0100361// HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the
362// information obtained from annotations within the source code in order to create the complete set
363// of flags that should be applied to the dex implementation jars on the bootclasspath.
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100364//
365// Each property contains a list of paths. With the exception of the Unsupported_packages the paths
366// of each property reference a plain text file that contains a java signature per line. The flags
367// for each of those signatures will be updated in a property specific way.
368//
369// The Unsupported_packages property contains a list of paths, each of which is a plain text file
370// with one Java package per line. All members of all classes within that package (but not nested
371// packages) will be updated in a property specific way.
Paul Duffin46169772021-04-14 15:01:56 +0100372type HiddenAPIFlagFileProperties struct {
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100373 // Marks each signature in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +0100374 Unsupported []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100375
376 // Marks each signature in the referenced files as being unsupported because it has been removed.
377 // Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +0100378 Removed []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100379
380 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R
381 // and low priority.
Paul Duffin702210b2021-04-08 20:12:41 +0100382 Max_target_r_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100383
384 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q.
Paul Duffin702210b2021-04-08 20:12:41 +0100385 Max_target_q []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100386
387 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P.
Paul Duffin702210b2021-04-08 20:12:41 +0100388 Max_target_p []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100389
390 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O
391 // and low priority. Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +0100392 Max_target_o_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100393
394 // Marks each signature in the referenced files as being blocked.
Paul Duffin702210b2021-04-08 20:12:41 +0100395 Blocked []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100396
397 // Marks each signature in every package in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +0100398 Unsupported_packages []string `android:"path"`
399}
400
Paul Duffine3dc6602021-04-14 09:50:43 +0100401type hiddenAPIFlagFileCategory struct {
Paul Duffin524c82c2021-06-09 14:39:28 +0100402 // PropertyName is the name of the property for this category.
403 PropertyName string
Paul Duffine3dc6602021-04-14 09:50:43 +0100404
Paul Duffincc17bfe2021-04-19 13:21:20 +0100405 // propertyValueReader retrieves the value of the property for this category from the set of
Paul Duffine3dc6602021-04-14 09:50:43 +0100406 // properties.
Paul Duffincc17bfe2021-04-19 13:21:20 +0100407 propertyValueReader func(properties *HiddenAPIFlagFileProperties) []string
Paul Duffine3dc6602021-04-14 09:50:43 +0100408
409 // commandMutator adds the appropriate command line options for this category to the supplied
410 // command
411 commandMutator func(command *android.RuleBuilderCommand, path android.Path)
412}
413
Paul Duffin32cf58a2021-05-18 16:32:50 +0100414// The flag file category for removed members of the API.
415//
Paul Duffin524c82c2021-06-09 14:39:28 +0100416// This is extracted from HiddenAPIFlagFileCategories as it is needed to add the dex signatures
Paul Duffin32cf58a2021-05-18 16:32:50 +0100417// list of removed API members that are generated automatically from the removed.txt files provided
418// by API stubs.
419var hiddenAPIRemovedFlagFileCategory = &hiddenAPIFlagFileCategory{
420 // See HiddenAPIFlagFileProperties.Removed
Paul Duffin524c82c2021-06-09 14:39:28 +0100421 PropertyName: "removed",
Paul Duffin32cf58a2021-05-18 16:32:50 +0100422 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
423 return properties.Removed
424 },
425 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
426 command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed")
427 },
428}
429
Paul Duffin524c82c2021-06-09 14:39:28 +0100430var HiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{
Paul Duffin46169772021-04-14 15:01:56 +0100431 // See HiddenAPIFlagFileProperties.Unsupported
Paul Duffine3dc6602021-04-14 09:50:43 +0100432 {
Paul Duffin524c82c2021-06-09 14:39:28 +0100433 PropertyName: "unsupported",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100434 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100435 return properties.Unsupported
436 },
437 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
438 command.FlagWithInput("--unsupported ", path)
439 },
440 },
Paul Duffin32cf58a2021-05-18 16:32:50 +0100441 hiddenAPIRemovedFlagFileCategory,
Paul Duffin46169772021-04-14 15:01:56 +0100442 // See HiddenAPIFlagFileProperties.Max_target_r_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100443 {
Paul Duffin524c82c2021-06-09 14:39:28 +0100444 PropertyName: "max_target_r_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100445 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100446 return properties.Max_target_r_low_priority
447 },
448 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
449 command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio")
450 },
451 },
Paul Duffin46169772021-04-14 15:01:56 +0100452 // See HiddenAPIFlagFileProperties.Max_target_q
Paul Duffine3dc6602021-04-14 09:50:43 +0100453 {
Paul Duffin524c82c2021-06-09 14:39:28 +0100454 PropertyName: "max_target_q",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100455 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100456 return properties.Max_target_q
457 },
458 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
459 command.FlagWithInput("--max-target-q ", path)
460 },
461 },
Paul Duffin46169772021-04-14 15:01:56 +0100462 // See HiddenAPIFlagFileProperties.Max_target_p
Paul Duffine3dc6602021-04-14 09:50:43 +0100463 {
Paul Duffin524c82c2021-06-09 14:39:28 +0100464 PropertyName: "max_target_p",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100465 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100466 return properties.Max_target_p
467 },
468 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
469 command.FlagWithInput("--max-target-p ", path)
470 },
471 },
Paul Duffin46169772021-04-14 15:01:56 +0100472 // See HiddenAPIFlagFileProperties.Max_target_o_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100473 {
Paul Duffin524c82c2021-06-09 14:39:28 +0100474 PropertyName: "max_target_o_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100475 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100476 return properties.Max_target_o_low_priority
477 },
478 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
479 command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio")
480 },
481 },
Paul Duffin46169772021-04-14 15:01:56 +0100482 // See HiddenAPIFlagFileProperties.Blocked
Paul Duffine3dc6602021-04-14 09:50:43 +0100483 {
Paul Duffin524c82c2021-06-09 14:39:28 +0100484 PropertyName: "blocked",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100485 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100486 return properties.Blocked
487 },
488 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
489 command.FlagWithInput("--blocked ", path)
490 },
491 },
Paul Duffin46169772021-04-14 15:01:56 +0100492 // See HiddenAPIFlagFileProperties.Unsupported_packages
Paul Duffine3dc6602021-04-14 09:50:43 +0100493 {
Paul Duffin524c82c2021-06-09 14:39:28 +0100494 PropertyName: "unsupported_packages",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100495 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100496 return properties.Unsupported_packages
497 },
498 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
499 command.FlagWithInput("--unsupported ", path).Flag("--packages ")
500 },
501 },
Paul Duffin702210b2021-04-08 20:12:41 +0100502}
503
Paul Duffin438eb572021-05-21 16:58:23 +0100504// FlagFilesByCategory maps a hiddenAPIFlagFileCategory to the paths to the files in that category.
505type FlagFilesByCategory map[*hiddenAPIFlagFileCategory]android.Paths
506
507// append appends the supplied flags files to the corresponding category in this map.
508func (s FlagFilesByCategory) append(other FlagFilesByCategory) {
Paul Duffin524c82c2021-06-09 14:39:28 +0100509 for _, category := range HiddenAPIFlagFileCategories {
Paul Duffin438eb572021-05-21 16:58:23 +0100510 s[category] = append(s[category], other[category]...)
511 }
512}
513
514// dedup removes duplicates in the flag files, while maintaining the order in which they were
515// appended.
516func (s FlagFilesByCategory) dedup() {
517 for category, paths := range s {
518 s[category] = android.FirstUniquePaths(paths)
519 }
520}
521
Paul Duffinaf99afa2021-05-21 22:18:56 +0100522// HiddenAPIInfo contains information provided by the hidden API processing.
Paul Duffin2fef1362021-04-15 13:32:00 +0100523//
Paul Duffinaf99afa2021-05-21 22:18:56 +0100524// That includes paths resolved from HiddenAPIFlagFileProperties and also generated by hidden API
525// processing.
526type HiddenAPIInfo struct {
Paul Duffin438eb572021-05-21 16:58:23 +0100527 // FlagFilesByCategory maps from the flag file category to the paths containing information for
528 // that category.
529 FlagFilesByCategory FlagFilesByCategory
Paul Duffin2fef1362021-04-15 13:32:00 +0100530
Paul Duffin31fad802021-06-18 18:14:25 +0100531 // The paths to the stub dex jars for each of the *HiddenAPIScope in hiddenAPIScopes.
532 TransitiveStubDexJarsByScope StubDexJarsByScope
Paul Duffin18cf1972021-05-21 22:46:59 +0100533
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100534 // The output from the hidden API processing needs to be made available to other modules.
535 HiddenAPIFlagOutput
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100536}
Paul Duffin702210b2021-04-08 20:12:41 +0100537
Paul Duffinf1b358c2021-05-17 07:38:47 +0100538func newHiddenAPIInfo() *HiddenAPIInfo {
539 info := HiddenAPIInfo{
Paul Duffin31fad802021-06-18 18:14:25 +0100540 FlagFilesByCategory: FlagFilesByCategory{},
541 TransitiveStubDexJarsByScope: StubDexJarsByScope{},
Paul Duffinf1b358c2021-05-17 07:38:47 +0100542 }
543 return &info
544}
545
546func (i *HiddenAPIInfo) mergeFromFragmentDeps(ctx android.ModuleContext, fragments []android.Module) {
547 // Merge all the information from the fragments. The fragments form a DAG so it is possible that
548 // this will introduce duplicates so they will be resolved after processing all the fragments.
549 for _, fragment := range fragments {
550 if ctx.OtherModuleHasProvider(fragment, HiddenAPIInfoProvider) {
551 info := ctx.OtherModuleProvider(fragment, HiddenAPIInfoProvider).(HiddenAPIInfo)
Paul Duffin31fad802021-06-18 18:14:25 +0100552 i.TransitiveStubDexJarsByScope.append(info.TransitiveStubDexJarsByScope)
Paul Duffinf1b358c2021-05-17 07:38:47 +0100553 }
554 }
555
556 // Dedup and sort paths.
Paul Duffin31fad802021-06-18 18:14:25 +0100557 i.TransitiveStubDexJarsByScope.dedupAndSort()
Paul Duffinf1b358c2021-05-17 07:38:47 +0100558}
559
Paul Duffinaf99afa2021-05-21 22:18:56 +0100560var HiddenAPIInfoProvider = blueprint.NewProvider(HiddenAPIInfo{})
Paul Duffin9b381ef2021-04-08 23:01:37 +0100561
Paul Duffin31fad802021-06-18 18:14:25 +0100562// StubDexJarsByScope maps a *HiddenAPIScope to the paths to stub dex jars appropriate for that
563// scope. See hiddenAPIScopes for a list of the acceptable *HiddenAPIScope values.
564type StubDexJarsByScope map[*HiddenAPIScope]android.Paths
Paul Duffin1352f7c2021-05-21 22:18:49 +0100565
Paul Duffin31fad802021-06-18 18:14:25 +0100566// append appends the API scope specific stub dex jar args to the corresponding scope in this
Paul Duffin1352f7c2021-05-21 22:18:49 +0100567// map.
Paul Duffin31fad802021-06-18 18:14:25 +0100568func (s StubDexJarsByScope) append(other StubDexJarsByScope) {
569 for _, scope := range hiddenAPIScopes {
570 s[scope] = append(s[scope], other[scope]...)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100571 }
572}
573
574// dedupAndSort removes duplicates in the stub dex jar paths and sorts them into a consistent and
575// deterministic order.
Paul Duffin31fad802021-06-18 18:14:25 +0100576func (s StubDexJarsByScope) dedupAndSort() {
577 for apiScope, paths := range s {
578 s[apiScope] = android.SortedUniquePaths(paths)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100579 }
580}
581
Paul Duffin5cca7c42021-05-26 10:16:01 +0100582// stubDexJarsForWidestAPIScope returns the stub dex jars for the widest API scope provided by this
583// map. The relative width of APIs is determined by their order in hiddenAPIScopes.
584func (s StubDexJarsByScope) stubDexJarsForWidestAPIScope() android.Paths {
585 for i := len(hiddenAPIScopes) - 1; i >= 0; i-- {
586 apiScope := hiddenAPIScopes[i]
587 stubsForAPIScope := s[apiScope]
588 if len(stubsForAPIScope) != 0 {
589 return stubsForAPIScope
590 }
591 }
592
593 return nil
594}
595
Paul Duffin1352f7c2021-05-21 22:18:49 +0100596// HiddenAPIFlagInput encapsulates information obtained from a module and its dependencies that are
597// needed for hidden API flag generation.
598type HiddenAPIFlagInput struct {
599 // FlagFilesByCategory contains the flag files that override the initial flags that are derived
600 // from the stub dex files.
601 FlagFilesByCategory FlagFilesByCategory
602
Paul Duffin31fad802021-06-18 18:14:25 +0100603 // StubDexJarsByScope contains the stub dex jars for different *HiddenAPIScope and which determine
Paul Duffin1352f7c2021-05-21 22:18:49 +0100604 // the initial flags for each dex member.
Paul Duffin31fad802021-06-18 18:14:25 +0100605 StubDexJarsByScope StubDexJarsByScope
Paul Duffinf1b358c2021-05-17 07:38:47 +0100606
Paul Duffin31fad802021-06-18 18:14:25 +0100607 // DependencyStubDexJarsByScope contains the stub dex jars provided by the fragments on which this
608 // depends. It is the result of merging HiddenAPIInfo.TransitiveStubDexJarsByScope from each
Paul Duffinf1b358c2021-05-17 07:38:47 +0100609 // fragment on which this depends.
Paul Duffin31fad802021-06-18 18:14:25 +0100610 DependencyStubDexJarsByScope StubDexJarsByScope
Paul Duffin32cf58a2021-05-18 16:32:50 +0100611
Paul Duffin5cca7c42021-05-26 10:16:01 +0100612 // AdditionalStubDexJarsByScope contains stub dex jars provided by other modules in addition to
613 // the ones that are obtained from fragments on which this depends.
614 //
615 // These are kept separate from stub dex jars in HiddenAPIFlagInput.DependencyStubDexJarsByScope
616 // as there are not propagated transitively to other fragments that depend on this.
617 AdditionalStubDexJarsByScope StubDexJarsByScope
618
Paul Duffin32cf58a2021-05-18 16:32:50 +0100619 // RemovedTxtFiles is the list of removed.txt files provided by java_sdk_library modules that are
620 // specified in the bootclasspath_fragment's stub_libs and contents properties.
621 RemovedTxtFiles android.Paths
Paul Duffin1352f7c2021-05-21 22:18:49 +0100622}
623
624// newHiddenAPIFlagInput creates a new initialize HiddenAPIFlagInput struct.
625func newHiddenAPIFlagInput() HiddenAPIFlagInput {
626 input := HiddenAPIFlagInput{
Paul Duffin31fad802021-06-18 18:14:25 +0100627 FlagFilesByCategory: FlagFilesByCategory{},
628 StubDexJarsByScope: StubDexJarsByScope{},
629 DependencyStubDexJarsByScope: StubDexJarsByScope{},
Paul Duffin5cca7c42021-05-26 10:16:01 +0100630 AdditionalStubDexJarsByScope: StubDexJarsByScope{},
Paul Duffin1352f7c2021-05-21 22:18:49 +0100631 }
632
633 return input
634}
635
Paul Duffin62370922021-05-23 16:55:37 +0100636// canPerformHiddenAPIProcessing determines whether hidden API processing should be performed.
637//
638// A temporary workaround to avoid existing bootclasspath_fragments that do not provide the
639// appropriate information needed for hidden API processing breaking the build.
640// TODO(b/179354495): Remove this workaround.
641func (i *HiddenAPIFlagInput) canPerformHiddenAPIProcessing(ctx android.ModuleContext, properties bootclasspathFragmentProperties) bool {
642 // Performing hidden API processing without stubs is not supported and it is unlikely to ever be
643 // required as the whole point of adding something to the bootclasspath fragment is to add it to
644 // the bootclasspath in order to be used by something else in the system. Without any stubs it
645 // cannot do that.
Paul Duffin31fad802021-06-18 18:14:25 +0100646 if len(i.StubDexJarsByScope) == 0 {
Paul Duffin62370922021-05-23 16:55:37 +0100647 return false
648 }
649
650 // Hidden API processing is always enabled in tests.
651 if ctx.Config().TestProductVariables != nil {
652 return true
653 }
654
655 // A module that has fragments should have access to the information it needs in order to perform
656 // hidden API processing.
657 if len(properties.Fragments) != 0 {
658 return true
659 }
660
661 // The art bootclasspath fragment does not depend on any other fragments but already supports
662 // hidden API processing.
663 imageName := proptools.String(properties.Image_name)
664 if imageName == "art" {
665 return true
666 }
667
668 // Disable it for everything else.
669 return false
670}
671
Paul Duffin1352f7c2021-05-21 22:18:49 +0100672// gatherStubLibInfo gathers information from the stub libs needed by hidden API processing from the
673// dependencies added in hiddenAPIAddStubLibDependencies.
674//
675// That includes paths to the stub dex jars as well as paths to the *removed.txt files.
676func (i *HiddenAPIFlagInput) gatherStubLibInfo(ctx android.ModuleContext, contents []android.Module) {
Paul Duffin31fad802021-06-18 18:14:25 +0100677 addFromModule := func(ctx android.ModuleContext, module android.Module, apiScope *HiddenAPIScope) {
678 sdkKind := apiScope.sdkKind
679 dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, sdkKind)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100680 if dexJar != nil {
Paul Duffin31fad802021-06-18 18:14:25 +0100681 i.StubDexJarsByScope[apiScope] = append(i.StubDexJarsByScope[apiScope], dexJar)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100682 }
Paul Duffin32cf58a2021-05-18 16:32:50 +0100683
684 if sdkLibrary, ok := module.(SdkLibraryDependency); ok {
Paul Duffin31fad802021-06-18 18:14:25 +0100685 removedTxtFile := sdkLibrary.SdkRemovedTxtFile(ctx, sdkKind)
Paul Duffin32cf58a2021-05-18 16:32:50 +0100686 i.RemovedTxtFiles = append(i.RemovedTxtFiles, removedTxtFile.AsPaths()...)
687 }
Paul Duffin1352f7c2021-05-21 22:18:49 +0100688 }
689
690 // If the contents includes any java_sdk_library modules then add them to the stubs.
691 for _, module := range contents {
692 if _, ok := module.(SdkLibraryDependency); ok {
Paul Duffin31fad802021-06-18 18:14:25 +0100693 // Add information for every possible API scope needed by hidden API.
694 for _, apiScope := range hiddenAPISdkLibrarySupportedScopes {
695 addFromModule(ctx, module, apiScope)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100696 }
697 }
698 }
699
Paul Duffind061d402021-06-07 21:36:01 +0100700 ctx.VisitDirectDeps(func(module android.Module) {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100701 tag := ctx.OtherModuleDependencyTag(module)
702 if hiddenAPIStubsTag, ok := tag.(hiddenAPIStubsDependencyTag); ok {
Paul Duffin31fad802021-06-18 18:14:25 +0100703 apiScope := hiddenAPIStubsTag.apiScope
Paul Duffin5cca7c42021-05-26 10:16:01 +0100704 if hiddenAPIStubsTag.fromAdditionalDependency {
705 dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, apiScope.sdkKind)
706 if dexJar != nil {
707 i.AdditionalStubDexJarsByScope[apiScope] = append(i.AdditionalStubDexJarsByScope[apiScope], dexJar)
708 }
709 } else {
710 addFromModule(ctx, module, apiScope)
711 }
Paul Duffin1352f7c2021-05-21 22:18:49 +0100712 }
713 })
714
715 // Normalize the paths, i.e. remove duplicates and sort.
Paul Duffin31fad802021-06-18 18:14:25 +0100716 i.StubDexJarsByScope.dedupAndSort()
717 i.DependencyStubDexJarsByScope.dedupAndSort()
Paul Duffin32cf58a2021-05-18 16:32:50 +0100718 i.RemovedTxtFiles = android.SortedUniquePaths(i.RemovedTxtFiles)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100719}
720
721// extractFlagFilesFromProperties extracts the paths to flag files that are specified in the
722// supplied properties and stores them in this struct.
723func (i *HiddenAPIFlagInput) extractFlagFilesFromProperties(ctx android.ModuleContext, p *HiddenAPIFlagFileProperties) {
Paul Duffin524c82c2021-06-09 14:39:28 +0100724 for _, category := range HiddenAPIFlagFileCategories {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100725 paths := android.PathsForModuleSrc(ctx, category.propertyValueReader(p))
726 i.FlagFilesByCategory[category] = paths
727 }
728}
729
Paul Duffin31fad802021-06-18 18:14:25 +0100730func (i *HiddenAPIFlagInput) transitiveStubDexJarsByScope() StubDexJarsByScope {
731 transitive := i.DependencyStubDexJarsByScope
732 transitive.append(i.StubDexJarsByScope)
Paul Duffinf1b358c2021-05-17 07:38:47 +0100733 return transitive
734}
735
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100736// HiddenAPIFlagOutput contains paths to output files from the hidden API flag generation for a
737// bootclasspath_fragment module.
738type HiddenAPIFlagOutput struct {
739 // The path to the generated stub-flags.csv file.
740 StubFlagsPath android.Path
741
742 // The path to the generated annotation-flags.csv file.
743 AnnotationFlagsPath android.Path
744
745 // The path to the generated metadata.csv file.
746 MetadataPath android.Path
747
748 // The path to the generated index.csv file.
749 IndexPath android.Path
750
751 // The path to the generated all-flags.csv file.
752 AllFlagsPath android.Path
753}
754
Paul Duffin5f148ca2021-06-02 17:24:22 +0100755// bootDexJarByModule is a map from base module name (without prebuilt_ prefix) to the boot dex
756// path.
757type bootDexJarByModule map[string]android.Path
758
759// addPath adds the path for a module to the map.
760func (b bootDexJarByModule) addPath(module android.Module, path android.Path) {
761 b[android.RemoveOptionalPrebuiltPrefix(module.Name())] = path
762}
763
Paul Duffine5218812021-06-07 13:28:19 +0100764// bootDexJars returns the boot dex jar paths sorted by their keys.
765func (b bootDexJarByModule) bootDexJars() android.Paths {
766 paths := android.Paths{}
767 for _, k := range android.SortedStringKeys(b) {
768 paths = append(paths, b[k])
769 }
770 return paths
771}
772
Paul Duffin7f872162021-06-17 19:33:24 +0100773// bootDexJarsWithoutCoverage returns the boot dex jar paths sorted by their keys without coverage
774// libraries if present.
775func (b bootDexJarByModule) bootDexJarsWithoutCoverage() android.Paths {
776 paths := android.Paths{}
777 for _, k := range android.SortedStringKeys(b) {
778 if k == "jacocoagent" {
779 continue
780 }
781 paths = append(paths, b[k])
782 }
783 return paths
784}
785
Paul Duffine5218812021-06-07 13:28:19 +0100786// HiddenAPIOutput encapsulates the output from the hidden API processing.
787type HiddenAPIOutput struct {
788 HiddenAPIFlagOutput
789
790 // The map from base module name to the path to the encoded boot dex file.
791 EncodedBootDexFilesByModule bootDexJarByModule
792}
793
Paul Duffindfa10832021-05-13 17:31:51 +0100794// pathForValidation creates a path of the same type as the supplied type but with a name of
795// <path>.valid.
796//
797// e.g. If path is an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv then this will return
798// an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv.valid
799func pathForValidation(ctx android.PathContext, path android.WritablePath) android.WritablePath {
800 extWithoutLeadingDot := strings.TrimPrefix(path.Ext(), ".")
801 return path.ReplaceExtension(ctx, extWithoutLeadingDot+".valid")
802}
803
Paul Duffin2fef1362021-04-15 13:32:00 +0100804// buildRuleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from
805// the flags from all the modules, the stub flags, augmented with some additional configuration
806// files.
Paul Duffin702210b2021-04-08 20:12:41 +0100807//
808// baseFlagsPath is the path to the flags file containing all the information from the stubs plus
809// an entry for every single member in the dex implementation jars of the individual modules. Every
810// signature in any of the other files MUST be included in this file.
811//
Paul Duffin537ea3d2021-05-14 10:38:00 +0100812// annotationFlags is the path to the annotation flags file generated from annotation information
813// in each module.
Paul Duffin702210b2021-04-08 20:12:41 +0100814//
Paul Duffinaf99afa2021-05-21 22:18:56 +0100815// hiddenAPIInfo is a struct containing paths to files that augment the information provided by
Paul Duffin537ea3d2021-05-14 10:38:00 +0100816// the annotationFlags.
Paul Duffin32cf58a2021-05-18 16:32:50 +0100817func buildRuleToGenerateHiddenApiFlags(ctx android.BuilderContext, name, desc string,
Paul Duffind061d402021-06-07 21:36:01 +0100818 outputPath android.WritablePath, baseFlagsPath android.Path, annotationFlagPaths android.Paths,
Paul Duffin32cf58a2021-05-18 16:32:50 +0100819 flagFilesByCategory FlagFilesByCategory, allFlagsPaths android.Paths, generatedRemovedDexSignatures android.OptionalPath) {
Paul Duffindfa10832021-05-13 17:31:51 +0100820
Paul Duffindfa10832021-05-13 17:31:51 +0100821 // Create the rule that will generate the flag files.
Paul Duffind3c15132021-04-21 22:12:35 +0100822 tempPath := tempPathForRestat(ctx, outputPath)
Paul Duffin702210b2021-04-08 20:12:41 +0100823 rule := android.NewRuleBuilder(pctx, ctx)
824 command := rule.Command().
825 BuiltTool("generate_hiddenapi_lists").
826 FlagWithInput("--csv ", baseFlagsPath).
Paul Duffind061d402021-06-07 21:36:01 +0100827 Inputs(annotationFlagPaths).
Paul Duffin702210b2021-04-08 20:12:41 +0100828 FlagWithOutput("--output ", tempPath)
829
Paul Duffine3dc6602021-04-14 09:50:43 +0100830 // Add the options for the different categories of flag files.
Paul Duffin524c82c2021-06-09 14:39:28 +0100831 for _, category := range HiddenAPIFlagFileCategories {
Paul Duffin438eb572021-05-21 16:58:23 +0100832 paths := flagFilesByCategory[category]
Paul Duffine3dc6602021-04-14 09:50:43 +0100833 for _, path := range paths {
834 category.commandMutator(command, path)
835 }
Paul Duffin702210b2021-04-08 20:12:41 +0100836 }
837
Paul Duffin32cf58a2021-05-18 16:32:50 +0100838 // If available then pass the automatically generated file containing dex signatures of removed
839 // API members to the rule so they can be marked as removed.
840 if generatedRemovedDexSignatures.Valid() {
841 hiddenAPIRemovedFlagFileCategory.commandMutator(command, generatedRemovedDexSignatures.Path())
842 }
843
Paul Duffin702210b2021-04-08 20:12:41 +0100844 commitChangeForRestat(rule, tempPath, outputPath)
845
Paul Duffin2e880972021-06-23 23:29:09 +0100846 // If there are flag files that have been generated by fragments on which this depends then use
847 // them to validate the flag file generated by the rules created by this method.
848 if len(allFlagsPaths) > 0 {
849 validFile := buildRuleValidateOverlappingCsvFiles(ctx, name, desc, outputPath, allFlagsPaths)
850
Paul Duffindfa10832021-05-13 17:31:51 +0100851 // Add the file that indicates that the file generated by this is valid.
852 //
853 // This will cause the validation rule above to be run any time that the output of this rule
854 // changes but the validation will run in parallel with other rules that depend on this file.
855 command.Validation(validFile)
856 }
857
Paul Duffin2fef1362021-04-15 13:32:00 +0100858 rule.Build(name, desc)
859}
860
Paul Duffin2e880972021-06-23 23:29:09 +0100861// buildRuleValidateOverlappingCsvFiles checks that the modular CSV files, i.e. the files generated
862// by the individual bootclasspath_fragment modules are subsets of the monolithic CSV file.
863func buildRuleValidateOverlappingCsvFiles(ctx android.BuilderContext, name string, desc string, monolithicFilePath android.WritablePath, modularFilePaths android.Paths) android.WritablePath {
864 // The file which is used to record that the flags file is valid.
865 validFile := pathForValidation(ctx, monolithicFilePath)
866
867 // Create a rule to validate the output from the following rule.
868 rule := android.NewRuleBuilder(pctx, ctx)
869 rule.Command().
870 BuiltTool("verify_overlaps").
871 Input(monolithicFilePath).
872 Inputs(modularFilePaths).
873 // If validation passes then update the file that records that.
874 Text("&& touch").Output(validFile)
875 rule.Build(name+"Validation", desc+" validation")
876
877 return validFile
878}
879
Paul Duffine5218812021-06-07 13:28:19 +0100880// hiddenAPIRulesForBootclasspathFragment will generate all the flags for a fragment of the
881// bootclasspath and then encode the flags into the boot dex files.
Paul Duffin2fef1362021-04-15 13:32:00 +0100882//
883// It takes:
884// * Map from android.SdkKind to stub dex jar paths defining the API for that sdk kind.
885// * The list of modules that are the contents of the fragment.
886// * The additional manually curated flag files to use.
887//
888// It generates:
889// * stub-flags.csv
890// * annotation-flags.csv
891// * metadata.csv
892// * index.csv
893// * all-flags.csv
Paul Duffine5218812021-06-07 13:28:19 +0100894// * encoded boot dex files
895func hiddenAPIRulesForBootclasspathFragment(ctx android.ModuleContext, contents []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput {
Paul Duffin2fef1362021-04-15 13:32:00 +0100896 hiddenApiSubDir := "modular-hiddenapi"
897
Paul Duffine5218812021-06-07 13:28:19 +0100898 // Gather information about the boot dex files for the boot libraries provided by this fragment.
899 bootDexInfoByModule := extractBootDexInfoFromModules(ctx, contents)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100900
901 // Generate the stub-flags.csv.
Paul Duffin2fef1362021-04-15 13:32:00 +0100902 stubFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "stub-flags.csv")
Paul Duffin2e880972021-06-23 23:29:09 +0100903 buildRuleToGenerateHiddenAPIStubFlagsFile(ctx, "modularHiddenAPIStubFlagsFile", "modular hiddenapi stub flags", stubFlagsCSV, bootDexInfoByModule.bootDexJars(), input, nil)
Paul Duffin2fef1362021-04-15 13:32:00 +0100904
Paul Duffin537ea3d2021-05-14 10:38:00 +0100905 // Extract the classes jars from the contents.
Paul Duffindd5993f2021-06-10 10:18:22 +0100906 classesJars := extractClassesJarsFromModules(contents)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100907
Paul Duffin2fef1362021-04-15 13:32:00 +0100908 // Generate the set of flags from the annotations in the source code.
909 annotationFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "annotation-flags.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100910 buildRuleToGenerateAnnotationFlags(ctx, "modular hiddenapi annotation flags", classesJars, stubFlagsCSV, annotationFlagsCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100911
912 // Generate the metadata from the annotations in the source code.
913 metadataCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "metadata.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100914 buildRuleToGenerateMetadata(ctx, "modular hiddenapi metadata", classesJars, stubFlagsCSV, metadataCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100915
Paul Duffin537ea3d2021-05-14 10:38:00 +0100916 // Generate the index file from the CSV files in the classes jars.
Paul Duffin2fef1362021-04-15 13:32:00 +0100917 indexCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "index.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100918 buildRuleToGenerateIndex(ctx, "modular hiddenapi index", classesJars, indexCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100919
Paul Duffinaf99afa2021-05-21 22:18:56 +0100920 // Removed APIs need to be marked and in order to do that the hiddenAPIInfo needs to specify files
Paul Duffin2fef1362021-04-15 13:32:00 +0100921 // containing dex signatures of all the removed APIs. In the monolithic files that is done by
922 // manually combining all the removed.txt files for each API and then converting them to dex
Paul Duffin32cf58a2021-05-18 16:32:50 +0100923 // signatures, see the combined-removed-dex module. This does that automatically by using the
924 // *removed.txt files retrieved from the java_sdk_library modules that are specified in the
925 // stub_libs and contents properties of a bootclasspath_fragment.
926 removedDexSignatures := buildRuleToGenerateRemovedDexSignatures(ctx, input.RemovedTxtFiles)
Paul Duffin2fef1362021-04-15 13:32:00 +0100927
928 // Generate the all-flags.csv which are the flags that will, in future, be encoded into the dex
929 // files.
Paul Duffine5218812021-06-07 13:28:19 +0100930 allFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "all-flags.csv")
Paul Duffind061d402021-06-07 21:36:01 +0100931 buildRuleToGenerateHiddenApiFlags(ctx, "modularHiddenApiAllFlags", "modular hiddenapi all flags", allFlagsCSV, stubFlagsCSV, android.Paths{annotationFlagsCSV}, input.FlagFilesByCategory, nil, removedDexSignatures)
Paul Duffine5218812021-06-07 13:28:19 +0100932
933 // Encode the flags into the boot dex files.
934 encodedBootDexJarsByModule := map[string]android.Path{}
935 outputDir := android.PathForModuleOut(ctx, "hiddenapi-modular/encoded").OutputPath
936 for _, name := range android.SortedStringKeys(bootDexInfoByModule) {
937 bootDexInfo := bootDexInfoByModule[name]
938 unencodedDex := bootDexInfo.path
939 encodedDex := hiddenAPIEncodeDex(ctx, unencodedDex, allFlagsCSV, bootDexInfo.uncompressDex, outputDir)
940 encodedBootDexJarsByModule[name] = encodedDex
941 }
Paul Duffin2fef1362021-04-15 13:32:00 +0100942
943 // Store the paths in the info for use by other modules and sdk snapshot generation.
Paul Duffine5218812021-06-07 13:28:19 +0100944 output := HiddenAPIOutput{
945 HiddenAPIFlagOutput: HiddenAPIFlagOutput{
946 StubFlagsPath: stubFlagsCSV,
947 AnnotationFlagsPath: annotationFlagsCSV,
948 MetadataPath: metadataCSV,
949 IndexPath: indexCSV,
950 AllFlagsPath: allFlagsCSV,
951 },
952 EncodedBootDexFilesByModule: encodedBootDexJarsByModule,
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100953 }
954 return &output
Paul Duffin702210b2021-04-08 20:12:41 +0100955}
Paul Duffin537ea3d2021-05-14 10:38:00 +0100956
Paul Duffin32cf58a2021-05-18 16:32:50 +0100957func buildRuleToGenerateRemovedDexSignatures(ctx android.ModuleContext, removedTxtFiles android.Paths) android.OptionalPath {
958 if len(removedTxtFiles) == 0 {
959 return android.OptionalPath{}
960 }
961
962 output := android.PathForModuleOut(ctx, "modular-hiddenapi/removed-dex-signatures.txt")
963
964 rule := android.NewRuleBuilder(pctx, ctx)
965 rule.Command().
966 BuiltTool("metalava").
967 Flag("--no-banner").
968 Inputs(removedTxtFiles).
969 FlagWithOutput("--dex-api ", output)
970 rule.Build("modular-hiddenapi-removed-dex-signatures", "modular hiddenapi removed dex signatures")
971 return android.OptionalPathForPath(output)
972}
973
Paul Duffindd5993f2021-06-10 10:18:22 +0100974// extractBootDexJarsFromModules extracts the boot dex jars from the supplied modules.
Paul Duffine5218812021-06-07 13:28:19 +0100975func extractBootDexJarsFromModules(ctx android.ModuleContext, contents []android.Module) bootDexJarByModule {
976 bootDexJars := bootDexJarByModule{}
Paul Duffin537ea3d2021-05-14 10:38:00 +0100977 for _, module := range contents {
Paul Duffindd5993f2021-06-10 10:18:22 +0100978 hiddenAPIModule := hiddenAPIModuleFromModule(ctx, module)
979 if hiddenAPIModule == nil {
980 continue
981 }
Paul Duffine5218812021-06-07 13:28:19 +0100982 bootDexJar := retrieveBootDexJarFromHiddenAPIModule(ctx, hiddenAPIModule)
983 bootDexJars.addPath(module, bootDexJar)
Paul Duffin537ea3d2021-05-14 10:38:00 +0100984 }
985 return bootDexJars
986}
987
Paul Duffindd5993f2021-06-10 10:18:22 +0100988func hiddenAPIModuleFromModule(ctx android.BaseModuleContext, module android.Module) hiddenAPIModule {
989 if hiddenAPIModule, ok := module.(hiddenAPIModule); ok {
990 return hiddenAPIModule
991 } else if _, ok := module.(*DexImport); ok {
992 // Ignore this for the purposes of hidden API processing
993 } else {
994 ctx.ModuleErrorf("module %s does not implement hiddenAPIModule", module)
995 }
996
997 return nil
998}
999
Paul Duffine5218812021-06-07 13:28:19 +01001000// bootDexInfo encapsulates both the path and uncompressDex status retrieved from a hiddenAPIModule.
1001type bootDexInfo struct {
1002 // The path to the dex jar that has not had hidden API flags encoded into it.
1003 path android.Path
1004
1005 // Indicates whether the dex jar needs uncompressing before encoding.
1006 uncompressDex bool
1007}
1008
1009// bootDexInfoByModule is a map from module name (as returned by module.Name()) to the boot dex
1010// path (as returned by hiddenAPIModule.bootDexJar()) and the uncompressDex flag.
1011type bootDexInfoByModule map[string]bootDexInfo
1012
1013// bootDexJars returns the boot dex jar paths sorted by their keys.
1014func (b bootDexInfoByModule) bootDexJars() android.Paths {
1015 paths := android.Paths{}
1016 for _, m := range android.SortedStringKeys(b) {
1017 paths = append(paths, b[m].path)
1018 }
1019 return paths
1020}
1021
1022// extractBootDexInfoFromModules extracts the boot dex jar and uncompress dex state from
1023// each of the supplied modules which must implement hiddenAPIModule.
1024func extractBootDexInfoFromModules(ctx android.ModuleContext, contents []android.Module) bootDexInfoByModule {
1025 bootDexJarsByModule := bootDexInfoByModule{}
1026 for _, module := range contents {
1027 hiddenAPIModule := module.(hiddenAPIModule)
1028 bootDexJar := retrieveBootDexJarFromHiddenAPIModule(ctx, hiddenAPIModule)
1029 bootDexJarsByModule[module.Name()] = bootDexInfo{
1030 path: bootDexJar,
1031 uncompressDex: *hiddenAPIModule.uncompressDex(),
1032 }
1033 }
1034
1035 return bootDexJarsByModule
1036}
1037
1038// retrieveBootDexJarFromHiddenAPIModule retrieves the boot dex jar from the hiddenAPIModule.
1039//
1040// If the module does not provide a boot dex jar, i.e. the returned boot dex jar is nil, then that
1041// create a fake path and either report an error immediately or defer reporting of the error until
1042// the path is actually used.
1043func retrieveBootDexJarFromHiddenAPIModule(ctx android.ModuleContext, module hiddenAPIModule) android.Path {
1044 bootDexJar := module.bootDexJar()
1045 if bootDexJar == nil {
1046 fake := android.PathForModuleOut(ctx, fmt.Sprintf("fake/boot-dex/%s.jar", module.Name()))
1047 bootDexJar = fake
1048
1049 handleMissingDexBootFile(ctx, module, fake)
1050 }
1051 return bootDexJar
1052}
1053
Paul Duffindd5993f2021-06-10 10:18:22 +01001054// extractClassesJarsFromModules extracts the class jars from the supplied modules.
1055func extractClassesJarsFromModules(contents []android.Module) android.Paths {
Paul Duffin537ea3d2021-05-14 10:38:00 +01001056 classesJars := android.Paths{}
1057 for _, module := range contents {
Paul Duffindd5993f2021-06-10 10:18:22 +01001058 classesJars = append(classesJars, retrieveClassesJarsFromModule(module)...)
Paul Duffin537ea3d2021-05-14 10:38:00 +01001059 }
1060 return classesJars
1061}
Paul Duffin5f148ca2021-06-02 17:24:22 +01001062
Paul Duffindd5993f2021-06-10 10:18:22 +01001063// retrieveClassesJarsFromModule retrieves the classes jars from the supplied module.
1064func retrieveClassesJarsFromModule(module android.Module) android.Paths {
1065 if hiddenAPIModule, ok := module.(hiddenAPIModule); ok {
1066 return hiddenAPIModule.classesJars()
1067 }
1068
1069 return nil
1070}
1071
Paul Duffin5f148ca2021-06-02 17:24:22 +01001072// deferReportingMissingBootDexJar returns true if a missing boot dex jar should not be reported by
1073// Soong but should instead only be reported in ninja if the file is actually built.
1074func deferReportingMissingBootDexJar(ctx android.ModuleContext, module android.Module) bool {
1075 // TODO(b/179354495): Remove this workaround when it is unnecessary.
1076 // Prebuilt modules like framework-wifi do not yet provide dex implementation jars. So,
1077 // create a fake one that will cause a build error only if it is used.
1078 if ctx.Config().AlwaysUsePrebuiltSdks() {
1079 return true
1080 }
1081
Paul Duffine5218812021-06-07 13:28:19 +01001082 // Any missing dependency should be allowed.
1083 if ctx.Config().AllowMissingDependencies() {
1084 return true
1085 }
1086
Paul Duffin5f148ca2021-06-02 17:24:22 +01001087 // This is called for both platform_bootclasspath and bootclasspath_fragment modules.
1088 //
1089 // A bootclasspath_fragment module should only use the APEX variant of source or prebuilt modules.
1090 // Ideally, a bootclasspath_fragment module should never have a platform variant created for it
1091 // but unfortunately, due to b/187910671 it does.
1092 //
1093 // That causes issues when obtaining a boot dex jar for a prebuilt module as a prebuilt module
1094 // used by a bootclasspath_fragment can only provide a boot dex jar when it is part of APEX, i.e.
1095 // has an APEX variant not a platform variant.
1096 //
1097 // There are some other situations when a prebuilt module used by a bootclasspath_fragment cannot
1098 // provide a boot dex jar:
1099 // 1. If the bootclasspath_fragment is not exported by the prebuilt_apex/apex_set module then it
1100 // does not have an APEX variant and only has a platform variant and neither do its content
1101 // modules.
1102 // 2. Some build configurations, e.g. setting TARGET_BUILD_USE_PREBUILT_SDKS causes all
1103 // java_sdk_library_import modules to be treated as preferred and as many of them are not part
1104 // of an apex they cannot provide a boot dex jar.
1105 //
1106 // The first case causes problems when the affected prebuilt modules are preferred but that is an
1107 // invalid configuration and it is ok for it to fail as the work to enable that is not yet
1108 // complete. The second case is used for building targets that do not use boot dex jars and so
1109 // deferring error reporting to ninja is fine as the affected ninja targets should never be built.
1110 // That is handled above.
1111 //
1112 // A platform_bootclasspath module can use libraries from both platform and APEX variants. Unlike
1113 // the bootclasspath_fragment it supports dex_import modules which provides the dex file. So, it
1114 // can obtain a boot dex jar from a prebuilt that is not part of an APEX. However, it is assumed
1115 // that if the library can be part of an APEX then it is the APEX variant that is used.
1116 //
1117 // This check handles the slightly different requirements of the bootclasspath_fragment and
1118 // platform_bootclasspath modules by only deferring error reporting for the platform variant of
1119 // a prebuilt modules that has other variants which are part of an APEX.
1120 //
1121 // TODO(b/187910671): Remove this once platform variants are no longer created unnecessarily.
1122 if android.IsModulePrebuilt(module) {
1123 if am, ok := module.(android.ApexModule); ok && am.InAnyApex() {
1124 apexInfo := ctx.OtherModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
1125 if apexInfo.IsForPlatform() {
1126 return true
1127 }
1128 }
1129 }
1130
1131 // A bootclasspath module that is part of a versioned sdk never provides a boot dex jar as there
1132 // is no equivalently versioned prebuilt APEX file from which it can be obtained. However,
1133 // versioned bootclasspath modules are processed by Soong so in order to avoid them causing build
1134 // failures missing boot dex jars need to be deferred.
1135 if android.IsModuleInVersionedSdk(ctx.Module()) {
1136 return true
1137 }
1138
1139 return false
1140}
1141
1142// handleMissingDexBootFile will either log a warning or create an error rule to create the fake
1143// file depending on the value returned from deferReportingMissingBootDexJar.
1144func handleMissingDexBootFile(ctx android.ModuleContext, module android.Module, fake android.WritablePath) {
1145 if deferReportingMissingBootDexJar(ctx, module) {
1146 // Create an error rule that pretends to create the output file but will actually fail if it
1147 // is run.
1148 ctx.Build(pctx, android.BuildParams{
1149 Rule: android.ErrorRule,
1150 Output: fake,
1151 Args: map[string]string{
1152 "error": fmt.Sprintf("missing dependencies: boot dex jar for %s", module),
1153 },
1154 })
1155 } else {
1156 ctx.ModuleErrorf("module %s does not provide a dex jar", module)
1157 }
1158}
1159
1160// retrieveEncodedBootDexJarFromModule returns a path to the boot dex jar from the supplied module's
1161// DexJarBuildPath() method.
1162//
1163// The returned path will usually be to a dex jar file that has been encoded with hidden API flags.
1164// However, under certain conditions, e.g. errors, or special build configurations it will return
1165// a path to a fake file.
1166func retrieveEncodedBootDexJarFromModule(ctx android.ModuleContext, module android.Module) android.Path {
1167 bootDexJar := module.(interface{ DexJarBuildPath() android.Path }).DexJarBuildPath()
1168 if bootDexJar == nil {
1169 fake := android.PathForModuleOut(ctx, fmt.Sprintf("fake/encoded-dex/%s.jar", module.Name()))
1170 bootDexJar = fake
1171
1172 handleMissingDexBootFile(ctx, module, fake)
1173 }
1174 return bootDexJar
1175}
1176
1177// extractEncodedDexJarsFromModules extracts the encoded dex jars from the supplied modules.
1178func extractEncodedDexJarsFromModules(ctx android.ModuleContext, contents []android.Module) bootDexJarByModule {
1179 encodedDexJarsByModuleName := bootDexJarByModule{}
1180 for _, module := range contents {
1181 path := retrieveEncodedBootDexJarFromModule(ctx, module)
1182 encodedDexJarsByModuleName.addPath(module, path)
1183 }
1184 return encodedDexJarsByModuleName
1185}