blob: b5969e904f70fe282edf6870902674a6b0206e40 [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 Duffinc6bb7cf2021-04-08 17:49:27 +010023)
24
25// Contains support for processing hiddenAPI in a modular fashion.
26
Paul Duffin74431d52021-04-21 14:10:42 +010027type hiddenAPIStubsDependencyTag struct {
28 blueprint.BaseDependencyTag
29 sdkKind android.SdkKind
30}
31
32func (b hiddenAPIStubsDependencyTag) ExcludeFromApexContents() {
33}
34
35func (b hiddenAPIStubsDependencyTag) ReplaceSourceWithPrebuilt() bool {
36 return false
37}
38
Paul Duffin976b0e52021-04-27 23:20:26 +010039func (b hiddenAPIStubsDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType {
40 // If the module is a java_sdk_library then treat it as if it was specific in the java_sdk_libs
41 // property, otherwise treat if it was specified in the java_header_libs property.
42 if javaSdkLibrarySdkMemberType.IsInstance(child) {
43 return javaSdkLibrarySdkMemberType
44 }
45
46 return javaHeaderLibsSdkMemberType
47}
48
49func (b hiddenAPIStubsDependencyTag) ExportMember() bool {
50 // Export the module added via this dependency tag from the sdk.
51 return true
52}
53
Paul Duffin74431d52021-04-21 14:10:42 +010054// Avoid having to make stubs content explicitly visible to dependent modules.
55//
56// This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules
57// with proper dependencies.
58// TODO(b/177892522): Remove this and add needed visibility.
59func (b hiddenAPIStubsDependencyTag) ExcludeFromVisibilityEnforcement() {
60}
61
62var _ android.ExcludeFromVisibilityEnforcementTag = hiddenAPIStubsDependencyTag{}
63var _ android.ReplaceSourceWithPrebuilt = hiddenAPIStubsDependencyTag{}
64var _ android.ExcludeFromApexContentsTag = hiddenAPIStubsDependencyTag{}
Paul Duffin976b0e52021-04-27 23:20:26 +010065var _ android.SdkMemberTypeDependencyTag = hiddenAPIStubsDependencyTag{}
Paul Duffin74431d52021-04-21 14:10:42 +010066
Paul Duffin3e7fcc32021-04-15 13:31:38 +010067// hiddenAPIRelevantSdkKinds lists all the android.SdkKind instances that are needed by the hidden
68// API processing.
69var hiddenAPIRelevantSdkKinds = []android.SdkKind{
70 android.SdkPublic,
71 android.SdkSystem,
72 android.SdkTest,
73 android.SdkCorePlatform,
74}
75
Paul Duffin74431d52021-04-21 14:10:42 +010076// hiddenAPIComputeMonolithicStubLibModules computes the set of module names that provide stubs
77// needed to produce the hidden API monolithic stub flags file.
78func hiddenAPIComputeMonolithicStubLibModules(config android.Config) map[android.SdkKind][]string {
79 var publicStubModules []string
80 var systemStubModules []string
81 var testStubModules []string
82 var corePlatformStubModules []string
83
84 if config.AlwaysUsePrebuiltSdks() {
85 // Build configuration mandates using prebuilt stub modules
86 publicStubModules = append(publicStubModules, "sdk_public_current_android")
87 systemStubModules = append(systemStubModules, "sdk_system_current_android")
88 testStubModules = append(testStubModules, "sdk_test_current_android")
89 } else {
90 // Use stub modules built from source
91 publicStubModules = append(publicStubModules, "android_stubs_current")
92 systemStubModules = append(systemStubModules, "android_system_stubs_current")
93 testStubModules = append(testStubModules, "android_test_stubs_current")
94 }
95 // We do not have prebuilts of the core platform api yet
96 corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs")
97
98 // Allow products to define their own stubs for custom product jars that apps can use.
99 publicStubModules = append(publicStubModules, config.ProductHiddenAPIStubs()...)
100 systemStubModules = append(systemStubModules, config.ProductHiddenAPIStubsSystem()...)
101 testStubModules = append(testStubModules, config.ProductHiddenAPIStubsTest()...)
102 if config.IsEnvTrue("EMMA_INSTRUMENT") {
Paul Duffin098c8782021-05-14 10:45:25 +0100103 // Add jacoco-stubs to public, system and test. It doesn't make any real difference as public
104 // allows everyone access but it is needed to ensure consistent flags between the
105 // bootclasspath fragment generated flags and the platform_bootclasspath generated flags.
Paul Duffin74431d52021-04-21 14:10:42 +0100106 publicStubModules = append(publicStubModules, "jacoco-stubs")
Paul Duffin098c8782021-05-14 10:45:25 +0100107 systemStubModules = append(systemStubModules, "jacoco-stubs")
108 testStubModules = append(testStubModules, "jacoco-stubs")
Paul Duffin74431d52021-04-21 14:10:42 +0100109 }
110
111 m := map[android.SdkKind][]string{}
112 m[android.SdkPublic] = publicStubModules
113 m[android.SdkSystem] = systemStubModules
114 m[android.SdkTest] = testStubModules
115 m[android.SdkCorePlatform] = corePlatformStubModules
116 return m
117}
118
119// hiddenAPIAddStubLibDependencies adds dependencies onto the modules specified in
120// sdkKindToStubLibModules. It adds them in a well known order and uses an SdkKind specific tag to
121// identify the source of the dependency.
122func hiddenAPIAddStubLibDependencies(ctx android.BottomUpMutatorContext, sdkKindToStubLibModules map[android.SdkKind][]string) {
123 module := ctx.Module()
124 for _, sdkKind := range hiddenAPIRelevantSdkKinds {
125 modules := sdkKindToStubLibModules[sdkKind]
126 ctx.AddDependency(module, hiddenAPIStubsDependencyTag{sdkKind: sdkKind}, modules...)
127 }
128}
129
Paul Duffin74431d52021-04-21 14:10:42 +0100130// hiddenAPIRetrieveDexJarBuildPath retrieves the DexJarBuildPath from the specified module, if
131// available, or reports an error.
Paul Duffin10931582021-04-25 10:13:54 +0100132func hiddenAPIRetrieveDexJarBuildPath(ctx android.ModuleContext, module android.Module, kind android.SdkKind) android.Path {
133 var dexJar android.Path
134 if sdkLibrary, ok := module.(SdkLibraryDependency); ok {
135 dexJar = sdkLibrary.SdkApiStubDexJar(ctx, kind)
136 } else if j, ok := module.(UsesLibraryDependency); ok {
137 dexJar = j.DexJarBuildPath()
Paul Duffin74431d52021-04-21 14:10:42 +0100138 } else {
139 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 +0100140 return nil
Paul Duffin74431d52021-04-21 14:10:42 +0100141 }
Paul Duffin10931582021-04-25 10:13:54 +0100142
143 if dexJar == nil {
144 ctx.ModuleErrorf("dependency %s does not provide a dex jar, consider setting compile_dex: true", module)
145 }
146 return dexJar
Paul Duffin74431d52021-04-21 14:10:42 +0100147}
148
149var sdkKindToHiddenapiListOption = map[android.SdkKind]string{
150 android.SdkPublic: "public-stub-classpath",
151 android.SdkSystem: "system-stub-classpath",
152 android.SdkTest: "test-stub-classpath",
153 android.SdkCorePlatform: "core-platform-stub-classpath",
154}
155
156// ruleToGenerateHiddenAPIStubFlagsFile creates a rule to create a hidden API stub flags file.
157//
158// The rule is initialized but not built so that the caller can modify it and select an appropriate
159// name.
Paul Duffin1352f7c2021-05-21 22:18:49 +0100160func ruleToGenerateHiddenAPIStubFlagsFile(ctx android.BuilderContext, outputPath android.WritablePath, bootDexJars android.Paths, input HiddenAPIFlagInput) *android.RuleBuilder {
Paul Duffin74431d52021-04-21 14:10:42 +0100161 // Singleton rule which applies hiddenapi on all boot class path dex files.
162 rule := android.NewRuleBuilder(pctx, ctx)
163
164 tempPath := tempPathForRestat(ctx, outputPath)
165
166 command := rule.Command().
167 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
168 Text("list").
169 FlagForEachInput("--boot-dex=", bootDexJars)
170
171 // Iterate over the sdk kinds in a fixed order.
172 for _, sdkKind := range hiddenAPIRelevantSdkKinds {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100173 paths := input.StubDexJarsByKind[sdkKind]
Paul Duffin74431d52021-04-21 14:10:42 +0100174 if len(paths) > 0 {
175 option := sdkKindToHiddenapiListOption[sdkKind]
176 command.FlagWithInputList("--"+option+"=", paths, ":")
177 }
178 }
179
180 // Add the output path.
181 command.FlagWithOutput("--out-api-flags=", tempPath)
182
183 commitChangeForRestat(rule, tempPath, outputPath)
184 return rule
185}
186
Paul Duffin46169772021-04-14 15:01:56 +0100187// HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the
188// information obtained from annotations within the source code in order to create the complete set
189// of flags that should be applied to the dex implementation jars on the bootclasspath.
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100190//
191// Each property contains a list of paths. With the exception of the Unsupported_packages the paths
192// of each property reference a plain text file that contains a java signature per line. The flags
193// for each of those signatures will be updated in a property specific way.
194//
195// The Unsupported_packages property contains a list of paths, each of which is a plain text file
196// with one Java package per line. All members of all classes within that package (but not nested
197// packages) will be updated in a property specific way.
Paul Duffin46169772021-04-14 15:01:56 +0100198type HiddenAPIFlagFileProperties struct {
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100199 // Marks each signature in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +0100200 Unsupported []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100201
202 // Marks each signature in the referenced files as being unsupported because it has been removed.
203 // Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +0100204 Removed []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100205
206 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R
207 // and low priority.
Paul Duffin702210b2021-04-08 20:12:41 +0100208 Max_target_r_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100209
210 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q.
Paul Duffin702210b2021-04-08 20:12:41 +0100211 Max_target_q []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100212
213 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P.
Paul Duffin702210b2021-04-08 20:12:41 +0100214 Max_target_p []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100215
216 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O
217 // and low priority. Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +0100218 Max_target_o_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100219
220 // Marks each signature in the referenced files as being blocked.
Paul Duffin702210b2021-04-08 20:12:41 +0100221 Blocked []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100222
223 // Marks each signature in every package in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +0100224 Unsupported_packages []string `android:"path"`
225}
226
Paul Duffine3dc6602021-04-14 09:50:43 +0100227type hiddenAPIFlagFileCategory struct {
228 // propertyName is the name of the property for this category.
229 propertyName string
230
Paul Duffincc17bfe2021-04-19 13:21:20 +0100231 // propertyValueReader retrieves the value of the property for this category from the set of
Paul Duffine3dc6602021-04-14 09:50:43 +0100232 // properties.
Paul Duffincc17bfe2021-04-19 13:21:20 +0100233 propertyValueReader func(properties *HiddenAPIFlagFileProperties) []string
Paul Duffine3dc6602021-04-14 09:50:43 +0100234
235 // commandMutator adds the appropriate command line options for this category to the supplied
236 // command
237 commandMutator func(command *android.RuleBuilderCommand, path android.Path)
238}
239
240var hiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{
Paul Duffin46169772021-04-14 15:01:56 +0100241 // See HiddenAPIFlagFileProperties.Unsupported
Paul Duffine3dc6602021-04-14 09:50:43 +0100242 {
243 propertyName: "unsupported",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100244 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100245 return properties.Unsupported
246 },
247 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
248 command.FlagWithInput("--unsupported ", path)
249 },
250 },
Paul Duffin46169772021-04-14 15:01:56 +0100251 // See HiddenAPIFlagFileProperties.Removed
Paul Duffine3dc6602021-04-14 09:50:43 +0100252 {
253 propertyName: "removed",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100254 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100255 return properties.Removed
256 },
257 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
258 command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed")
259 },
260 },
Paul Duffin46169772021-04-14 15:01:56 +0100261 // See HiddenAPIFlagFileProperties.Max_target_r_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100262 {
263 propertyName: "max_target_r_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100264 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100265 return properties.Max_target_r_low_priority
266 },
267 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
268 command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio")
269 },
270 },
Paul Duffin46169772021-04-14 15:01:56 +0100271 // See HiddenAPIFlagFileProperties.Max_target_q
Paul Duffine3dc6602021-04-14 09:50:43 +0100272 {
273 propertyName: "max_target_q",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100274 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100275 return properties.Max_target_q
276 },
277 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
278 command.FlagWithInput("--max-target-q ", path)
279 },
280 },
Paul Duffin46169772021-04-14 15:01:56 +0100281 // See HiddenAPIFlagFileProperties.Max_target_p
Paul Duffine3dc6602021-04-14 09:50:43 +0100282 {
283 propertyName: "max_target_p",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100284 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100285 return properties.Max_target_p
286 },
287 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
288 command.FlagWithInput("--max-target-p ", path)
289 },
290 },
Paul Duffin46169772021-04-14 15:01:56 +0100291 // See HiddenAPIFlagFileProperties.Max_target_o_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100292 {
293 propertyName: "max_target_o_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100294 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100295 return properties.Max_target_o_low_priority
296 },
297 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
298 command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio")
299 },
300 },
Paul Duffin46169772021-04-14 15:01:56 +0100301 // See HiddenAPIFlagFileProperties.Blocked
Paul Duffine3dc6602021-04-14 09:50:43 +0100302 {
303 propertyName: "blocked",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100304 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100305 return properties.Blocked
306 },
307 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
308 command.FlagWithInput("--blocked ", path)
309 },
310 },
Paul Duffin46169772021-04-14 15:01:56 +0100311 // See HiddenAPIFlagFileProperties.Unsupported_packages
Paul Duffine3dc6602021-04-14 09:50:43 +0100312 {
313 propertyName: "unsupported_packages",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100314 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100315 return properties.Unsupported_packages
316 },
317 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
318 command.FlagWithInput("--unsupported ", path).Flag("--packages ")
319 },
320 },
Paul Duffin702210b2021-04-08 20:12:41 +0100321}
322
Paul Duffin438eb572021-05-21 16:58:23 +0100323// FlagFilesByCategory maps a hiddenAPIFlagFileCategory to the paths to the files in that category.
324type FlagFilesByCategory map[*hiddenAPIFlagFileCategory]android.Paths
325
326// append appends the supplied flags files to the corresponding category in this map.
327func (s FlagFilesByCategory) append(other FlagFilesByCategory) {
328 for _, category := range hiddenAPIFlagFileCategories {
329 s[category] = append(s[category], other[category]...)
330 }
331}
332
333// dedup removes duplicates in the flag files, while maintaining the order in which they were
334// appended.
335func (s FlagFilesByCategory) dedup() {
336 for category, paths := range s {
337 s[category] = android.FirstUniquePaths(paths)
338 }
339}
340
Paul Duffin2fef1362021-04-15 13:32:00 +0100341// hiddenAPIFlagFileInfo contains paths resolved from HiddenAPIFlagFileProperties and also generated
342// by hidden API processing.
343//
344// This is used both for an individual bootclasspath_fragment to provide it to other modules and
345// for a module to collate the files from the fragments it depends upon. That is why the fields are
346// all Paths even though they are initialized with a single path.
Paul Duffin46169772021-04-14 15:01:56 +0100347type hiddenAPIFlagFileInfo struct {
Paul Duffin438eb572021-05-21 16:58:23 +0100348 // FlagFilesByCategory maps from the flag file category to the paths containing information for
349 // that category.
350 FlagFilesByCategory FlagFilesByCategory
Paul Duffin2fef1362021-04-15 13:32:00 +0100351
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100352 // The output from the hidden API processing needs to be made available to other modules.
353 HiddenAPIFlagOutput
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100354}
Paul Duffin702210b2021-04-08 20:12:41 +0100355
Paul Duffin9b381ef2021-04-08 23:01:37 +0100356var hiddenAPIFlagFileInfoProvider = blueprint.NewProvider(hiddenAPIFlagFileInfo{})
357
Paul Duffin1352f7c2021-05-21 22:18:49 +0100358// StubDexJarsByKind maps an android.SdkKind to the paths to stub dex jars appropriate for that
359// level. See hiddenAPIRelevantSdkKinds for a list of the acceptable android.SdkKind values.
360type StubDexJarsByKind map[android.SdkKind]android.Paths
361
362// append appends the supplied kind specific stub dex jar pargs to the corresponding kind in this
363// map.
364func (s StubDexJarsByKind) append(other StubDexJarsByKind) {
365 for _, kind := range hiddenAPIRelevantSdkKinds {
366 s[kind] = append(s[kind], other[kind]...)
367 }
368}
369
370// dedupAndSort removes duplicates in the stub dex jar paths and sorts them into a consistent and
371// deterministic order.
372func (s StubDexJarsByKind) dedupAndSort() {
373 for kind, paths := range s {
374 s[kind] = android.SortedUniquePaths(paths)
375 }
376}
377
378// HiddenAPIFlagInput encapsulates information obtained from a module and its dependencies that are
379// needed for hidden API flag generation.
380type HiddenAPIFlagInput struct {
381 // FlagFilesByCategory contains the flag files that override the initial flags that are derived
382 // from the stub dex files.
383 FlagFilesByCategory FlagFilesByCategory
384
385 // StubDexJarsByKind contains the stub dex jars for different android.SdkKind and which determine
386 // the initial flags for each dex member.
387 StubDexJarsByKind StubDexJarsByKind
388}
389
390// newHiddenAPIFlagInput creates a new initialize HiddenAPIFlagInput struct.
391func newHiddenAPIFlagInput() HiddenAPIFlagInput {
392 input := HiddenAPIFlagInput{
393 FlagFilesByCategory: FlagFilesByCategory{},
394 StubDexJarsByKind: StubDexJarsByKind{},
395 }
396
397 return input
398}
399
400// gatherStubLibInfo gathers information from the stub libs needed by hidden API processing from the
401// dependencies added in hiddenAPIAddStubLibDependencies.
402//
403// That includes paths to the stub dex jars as well as paths to the *removed.txt files.
404func (i *HiddenAPIFlagInput) gatherStubLibInfo(ctx android.ModuleContext, contents []android.Module) {
405 addFromModule := func(ctx android.ModuleContext, module android.Module, kind android.SdkKind) {
406 dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, kind)
407 if dexJar != nil {
408 i.StubDexJarsByKind[kind] = append(i.StubDexJarsByKind[kind], dexJar)
409 }
410 }
411
412 // If the contents includes any java_sdk_library modules then add them to the stubs.
413 for _, module := range contents {
414 if _, ok := module.(SdkLibraryDependency); ok {
415 // Add information for every possible kind needed by hidden API. SdkCorePlatform is not used
416 // as the java_sdk_library does not have special support for core_platform API, instead it is
417 // implemented as a customized form of SdkPublic.
418 for _, kind := range []android.SdkKind{android.SdkPublic, android.SdkSystem, android.SdkTest} {
419 addFromModule(ctx, module, kind)
420 }
421 }
422 }
423
424 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
425 tag := ctx.OtherModuleDependencyTag(module)
426 if hiddenAPIStubsTag, ok := tag.(hiddenAPIStubsDependencyTag); ok {
427 kind := hiddenAPIStubsTag.sdkKind
428 addFromModule(ctx, module, kind)
429 }
430 })
431
432 // Normalize the paths, i.e. remove duplicates and sort.
433 i.StubDexJarsByKind.dedupAndSort()
434}
435
436// extractFlagFilesFromProperties extracts the paths to flag files that are specified in the
437// supplied properties and stores them in this struct.
438func (i *HiddenAPIFlagInput) extractFlagFilesFromProperties(ctx android.ModuleContext, p *HiddenAPIFlagFileProperties) {
439 for _, category := range hiddenAPIFlagFileCategories {
440 paths := android.PathsForModuleSrc(ctx, category.propertyValueReader(p))
441 i.FlagFilesByCategory[category] = paths
442 }
443}
444
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100445// HiddenAPIFlagOutput contains paths to output files from the hidden API flag generation for a
446// bootclasspath_fragment module.
447type HiddenAPIFlagOutput struct {
448 // The path to the generated stub-flags.csv file.
449 StubFlagsPath android.Path
450
451 // The path to the generated annotation-flags.csv file.
452 AnnotationFlagsPath android.Path
453
454 // The path to the generated metadata.csv file.
455 MetadataPath android.Path
456
457 // The path to the generated index.csv file.
458 IndexPath android.Path
459
460 // The path to the generated all-flags.csv file.
461 AllFlagsPath android.Path
462}
463
Paul Duffindfa10832021-05-13 17:31:51 +0100464// pathForValidation creates a path of the same type as the supplied type but with a name of
465// <path>.valid.
466//
467// e.g. If path is an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv then this will return
468// an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv.valid
469func pathForValidation(ctx android.PathContext, path android.WritablePath) android.WritablePath {
470 extWithoutLeadingDot := strings.TrimPrefix(path.Ext(), ".")
471 return path.ReplaceExtension(ctx, extWithoutLeadingDot+".valid")
472}
473
Paul Duffin2fef1362021-04-15 13:32:00 +0100474// buildRuleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from
475// the flags from all the modules, the stub flags, augmented with some additional configuration
476// files.
Paul Duffin702210b2021-04-08 20:12:41 +0100477//
478// baseFlagsPath is the path to the flags file containing all the information from the stubs plus
479// an entry for every single member in the dex implementation jars of the individual modules. Every
480// signature in any of the other files MUST be included in this file.
481//
Paul Duffin537ea3d2021-05-14 10:38:00 +0100482// annotationFlags is the path to the annotation flags file generated from annotation information
483// in each module.
Paul Duffin702210b2021-04-08 20:12:41 +0100484//
Paul Duffin537ea3d2021-05-14 10:38:00 +0100485// flagFileInfo is a struct containing paths to files that augment the information provided by
486// the annotationFlags.
Paul Duffin438eb572021-05-21 16:58:23 +0100487func buildRuleToGenerateHiddenApiFlags(ctx android.BuilderContext, name, desc string, outputPath android.WritablePath, baseFlagsPath android.Path, annotationFlags android.Path, flagFilesByCategory FlagFilesByCategory, allFlagsPaths android.Paths) {
Paul Duffindfa10832021-05-13 17:31:51 +0100488
489 // The file which is used to record that the flags file is valid.
490 var validFile android.WritablePath
491
492 // If there are flag files that have been generated by fragments on which this depends then use
493 // them to validate the flag file generated by the rules created by this method.
Paul Duffin438eb572021-05-21 16:58:23 +0100494 if len(allFlagsPaths) > 0 {
Paul Duffindfa10832021-05-13 17:31:51 +0100495 // The flags file generated by the rule created by this method needs to be validated to ensure
496 // that it is consistent with the flag files generated by the individual fragments.
497
498 validFile = pathForValidation(ctx, outputPath)
499
500 // Create a rule to validate the output from the following rule.
501 rule := android.NewRuleBuilder(pctx, ctx)
502 rule.Command().
503 BuiltTool("verify_overlaps").
504 Input(outputPath).
505 Inputs(allFlagsPaths).
506 // If validation passes then update the file that records that.
507 Text("&& touch").Output(validFile)
508 rule.Build(name+"Validation", desc+" validation")
509 }
510
511 // Create the rule that will generate the flag files.
Paul Duffind3c15132021-04-21 22:12:35 +0100512 tempPath := tempPathForRestat(ctx, outputPath)
Paul Duffin702210b2021-04-08 20:12:41 +0100513 rule := android.NewRuleBuilder(pctx, ctx)
514 command := rule.Command().
515 BuiltTool("generate_hiddenapi_lists").
516 FlagWithInput("--csv ", baseFlagsPath).
Paul Duffin537ea3d2021-05-14 10:38:00 +0100517 Input(annotationFlags).
Paul Duffin702210b2021-04-08 20:12:41 +0100518 FlagWithOutput("--output ", tempPath)
519
Paul Duffine3dc6602021-04-14 09:50:43 +0100520 // Add the options for the different categories of flag files.
521 for _, category := range hiddenAPIFlagFileCategories {
Paul Duffin438eb572021-05-21 16:58:23 +0100522 paths := flagFilesByCategory[category]
Paul Duffine3dc6602021-04-14 09:50:43 +0100523 for _, path := range paths {
524 category.commandMutator(command, path)
525 }
Paul Duffin702210b2021-04-08 20:12:41 +0100526 }
527
528 commitChangeForRestat(rule, tempPath, outputPath)
529
Paul Duffindfa10832021-05-13 17:31:51 +0100530 if validFile != nil {
531 // Add the file that indicates that the file generated by this is valid.
532 //
533 // This will cause the validation rule above to be run any time that the output of this rule
534 // changes but the validation will run in parallel with other rules that depend on this file.
535 command.Validation(validFile)
536 }
537
Paul Duffin2fef1362021-04-15 13:32:00 +0100538 rule.Build(name, desc)
539}
540
541// hiddenAPIGenerateAllFlagsForBootclasspathFragment will generate all the flags for a fragment
542// of the bootclasspath.
543//
544// It takes:
545// * Map from android.SdkKind to stub dex jar paths defining the API for that sdk kind.
546// * The list of modules that are the contents of the fragment.
547// * The additional manually curated flag files to use.
548//
549// It generates:
550// * stub-flags.csv
551// * annotation-flags.csv
552// * metadata.csv
553// * index.csv
554// * all-flags.csv
Paul Duffin1352f7c2021-05-21 22:18:49 +0100555func hiddenAPIGenerateAllFlagsForBootclasspathFragment(ctx android.ModuleContext, contents []hiddenAPIModule, input HiddenAPIFlagInput) *HiddenAPIFlagOutput {
Paul Duffin2fef1362021-04-15 13:32:00 +0100556 hiddenApiSubDir := "modular-hiddenapi"
557
Paul Duffin1352f7c2021-05-21 22:18:49 +0100558 // Gather the dex files for the boot libraries provided by this fragment.
Paul Duffin537ea3d2021-05-14 10:38:00 +0100559 bootDexJars := extractBootDexJarsFromHiddenAPIModules(ctx, contents)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100560
561 // Generate the stub-flags.csv.
Paul Duffin2fef1362021-04-15 13:32:00 +0100562 stubFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "stub-flags.csv")
Paul Duffin1352f7c2021-05-21 22:18:49 +0100563 rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, stubFlagsCSV, bootDexJars, input)
Paul Duffin2fef1362021-04-15 13:32:00 +0100564 rule.Build("modularHiddenAPIStubFlagsFile", "modular hiddenapi stub flags")
565
Paul Duffin537ea3d2021-05-14 10:38:00 +0100566 // Extract the classes jars from the contents.
567 classesJars := extractClassJarsFromHiddenAPIModules(ctx, contents)
568
Paul Duffin2fef1362021-04-15 13:32:00 +0100569 // Generate the set of flags from the annotations in the source code.
570 annotationFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "annotation-flags.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100571 buildRuleToGenerateAnnotationFlags(ctx, "modular hiddenapi annotation flags", classesJars, stubFlagsCSV, annotationFlagsCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100572
573 // Generate the metadata from the annotations in the source code.
574 metadataCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "metadata.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100575 buildRuleToGenerateMetadata(ctx, "modular hiddenapi metadata", classesJars, stubFlagsCSV, metadataCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100576
Paul Duffin537ea3d2021-05-14 10:38:00 +0100577 // Generate the index file from the CSV files in the classes jars.
Paul Duffin2fef1362021-04-15 13:32:00 +0100578 indexCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "index.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100579 buildRuleToGenerateIndex(ctx, "modular hiddenapi index", classesJars, indexCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100580
581 // Removed APIs need to be marked and in order to do that the flagFileInfo needs to specify files
582 // containing dex signatures of all the removed APIs. In the monolithic files that is done by
583 // manually combining all the removed.txt files for each API and then converting them to dex
584 // signatures, see the combined-removed-dex module. That will all be done automatically in future.
585 // For now removed APIs are ignored.
586 // TODO(b/179354495): handle removed apis automatically.
587
588 // Generate the all-flags.csv which are the flags that will, in future, be encoded into the dex
589 // files.
590 outputPath := android.PathForModuleOut(ctx, hiddenApiSubDir, "all-flags.csv")
Paul Duffin1352f7c2021-05-21 22:18:49 +0100591 buildRuleToGenerateHiddenApiFlags(ctx, "modularHiddenApiAllFlags", "modular hiddenapi all flags", outputPath, stubFlagsCSV, annotationFlagsCSV, input.FlagFilesByCategory, nil)
Paul Duffin2fef1362021-04-15 13:32:00 +0100592
593 // Store the paths in the info for use by other modules and sdk snapshot generation.
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100594 output := HiddenAPIFlagOutput{
595 StubFlagsPath: stubFlagsCSV,
596 AnnotationFlagsPath: annotationFlagsCSV,
597 MetadataPath: metadataCSV,
598 IndexPath: indexCSV,
599 AllFlagsPath: outputPath,
600 }
601 return &output
Paul Duffin702210b2021-04-08 20:12:41 +0100602}
Paul Duffin537ea3d2021-05-14 10:38:00 +0100603
604// gatherHiddenAPIModuleFromContents gathers the hiddenAPIModule from the supplied contents.
605func gatherHiddenAPIModuleFromContents(ctx android.ModuleContext, contents []android.Module) []hiddenAPIModule {
606 hiddenAPIModules := []hiddenAPIModule{}
607 for _, module := range contents {
608 if hiddenAPI, ok := module.(hiddenAPIModule); ok {
609 hiddenAPIModules = append(hiddenAPIModules, hiddenAPI)
610 } else if _, ok := module.(*DexImport); ok {
611 // Ignore this for the purposes of hidden API processing
612 } else {
613 ctx.ModuleErrorf("module %s does not implement hiddenAPIModule", module)
614 }
615 }
616 return hiddenAPIModules
617}
618
619// extractBootDexJarsFromHiddenAPIModules extracts the boot dex jars from the supplied modules.
620func extractBootDexJarsFromHiddenAPIModules(ctx android.ModuleContext, contents []hiddenAPIModule) android.Paths {
621 bootDexJars := android.Paths{}
622 for _, module := range contents {
623 bootDexJar := module.bootDexJar()
624 if bootDexJar == nil {
Paul Duffin7487a7a2021-05-19 09:36:09 +0100625 if ctx.Config().AlwaysUsePrebuiltSdks() {
626 // TODO(b/179354495): Remove this work around when it is unnecessary.
627 // Prebuilt modules like framework-wifi do not yet provide dex implementation jars. So,
628 // create a fake one that will cause a build error only if it is used.
629 fake := android.PathForModuleOut(ctx, "fake/boot-dex/%s.jar", module.Name())
630
631 // Create an error rule that pretends to create the output file but will actually fail if it
632 // is run.
633 ctx.Build(pctx, android.BuildParams{
634 Rule: android.ErrorRule,
635 Output: fake,
636 Args: map[string]string{
637 "error": fmt.Sprintf("missing dependencies: boot dex jar for %s", module),
638 },
639 })
640 bootDexJars = append(bootDexJars, fake)
641 } else {
642 ctx.ModuleErrorf("module %s does not provide a dex jar", module)
643 }
Paul Duffin537ea3d2021-05-14 10:38:00 +0100644 } else {
645 bootDexJars = append(bootDexJars, bootDexJar)
646 }
647 }
648 return bootDexJars
649}
650
651// extractClassJarsFromHiddenAPIModules extracts the class jars from the supplied modules.
652func extractClassJarsFromHiddenAPIModules(ctx android.ModuleContext, contents []hiddenAPIModule) android.Paths {
653 classesJars := android.Paths{}
654 for _, module := range contents {
655 classesJars = append(classesJars, module.classesJars()...)
656 }
657 return classesJars
658}