blob: 8daf197c6742a0f538a41a4a1314c01428b102e2 [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 (
18 "android/soong/android"
Paul Duffin9b381ef2021-04-08 23:01:37 +010019 "github.com/google/blueprint"
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010020)
21
22// Contains support for processing hiddenAPI in a modular fashion.
23
Paul Duffin3e7fcc32021-04-15 13:31:38 +010024// hiddenAPIRelevantSdkKinds lists all the android.SdkKind instances that are needed by the hidden
25// API processing.
26var hiddenAPIRelevantSdkKinds = []android.SdkKind{
27 android.SdkPublic,
28 android.SdkSystem,
29 android.SdkTest,
30 android.SdkCorePlatform,
31}
32
Paul Duffin46169772021-04-14 15:01:56 +010033// HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the
34// information obtained from annotations within the source code in order to create the complete set
35// of flags that should be applied to the dex implementation jars on the bootclasspath.
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010036//
37// Each property contains a list of paths. With the exception of the Unsupported_packages the paths
38// of each property reference a plain text file that contains a java signature per line. The flags
39// for each of those signatures will be updated in a property specific way.
40//
41// The Unsupported_packages property contains a list of paths, each of which is a plain text file
42// with one Java package per line. All members of all classes within that package (but not nested
43// packages) will be updated in a property specific way.
Paul Duffin46169772021-04-14 15:01:56 +010044type HiddenAPIFlagFileProperties struct {
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010045 // Marks each signature in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +010046 Unsupported []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010047
48 // Marks each signature in the referenced files as being unsupported because it has been removed.
49 // Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +010050 Removed []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010051
52 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R
53 // and low priority.
Paul Duffin702210b2021-04-08 20:12:41 +010054 Max_target_r_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010055
56 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q.
Paul Duffin702210b2021-04-08 20:12:41 +010057 Max_target_q []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010058
59 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P.
Paul Duffin702210b2021-04-08 20:12:41 +010060 Max_target_p []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010061
62 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O
63 // and low priority. Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +010064 Max_target_o_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010065
66 // Marks each signature in the referenced files as being blocked.
Paul Duffin702210b2021-04-08 20:12:41 +010067 Blocked []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010068
69 // Marks each signature in every package in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +010070 Unsupported_packages []string `android:"path"`
71}
72
Paul Duffin46169772021-04-14 15:01:56 +010073func (p *HiddenAPIFlagFileProperties) hiddenAPIFlagFileInfo(ctx android.ModuleContext) hiddenAPIFlagFileInfo {
74 info := hiddenAPIFlagFileInfo{categoryToPaths: map[*hiddenAPIFlagFileCategory]android.Paths{}}
Paul Duffine3dc6602021-04-14 09:50:43 +010075 for _, category := range hiddenAPIFlagFileCategories {
Paul Duffincc17bfe2021-04-19 13:21:20 +010076 paths := android.PathsForModuleSrc(ctx, category.propertyValueReader(p))
Paul Duffine3dc6602021-04-14 09:50:43 +010077 info.categoryToPaths[category] = paths
Paul Duffin702210b2021-04-08 20:12:41 +010078 }
Paul Duffine3dc6602021-04-14 09:50:43 +010079 return info
80}
81
82type hiddenAPIFlagFileCategory struct {
83 // propertyName is the name of the property for this category.
84 propertyName string
85
Paul Duffincc17bfe2021-04-19 13:21:20 +010086 // propertyValueReader retrieves the value of the property for this category from the set of
Paul Duffine3dc6602021-04-14 09:50:43 +010087 // properties.
Paul Duffincc17bfe2021-04-19 13:21:20 +010088 propertyValueReader func(properties *HiddenAPIFlagFileProperties) []string
Paul Duffine3dc6602021-04-14 09:50:43 +010089
90 // commandMutator adds the appropriate command line options for this category to the supplied
91 // command
92 commandMutator func(command *android.RuleBuilderCommand, path android.Path)
93}
94
95var hiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{
Paul Duffin46169772021-04-14 15:01:56 +010096 // See HiddenAPIFlagFileProperties.Unsupported
Paul Duffine3dc6602021-04-14 09:50:43 +010097 {
98 propertyName: "unsupported",
Paul Duffincc17bfe2021-04-19 13:21:20 +010099 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100100 return properties.Unsupported
101 },
102 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
103 command.FlagWithInput("--unsupported ", path)
104 },
105 },
Paul Duffin46169772021-04-14 15:01:56 +0100106 // See HiddenAPIFlagFileProperties.Removed
Paul Duffine3dc6602021-04-14 09:50:43 +0100107 {
108 propertyName: "removed",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100109 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100110 return properties.Removed
111 },
112 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
113 command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed")
114 },
115 },
Paul Duffin46169772021-04-14 15:01:56 +0100116 // See HiddenAPIFlagFileProperties.Max_target_r_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100117 {
118 propertyName: "max_target_r_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100119 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100120 return properties.Max_target_r_low_priority
121 },
122 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
123 command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio")
124 },
125 },
Paul Duffin46169772021-04-14 15:01:56 +0100126 // See HiddenAPIFlagFileProperties.Max_target_q
Paul Duffine3dc6602021-04-14 09:50:43 +0100127 {
128 propertyName: "max_target_q",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100129 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100130 return properties.Max_target_q
131 },
132 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
133 command.FlagWithInput("--max-target-q ", path)
134 },
135 },
Paul Duffin46169772021-04-14 15:01:56 +0100136 // See HiddenAPIFlagFileProperties.Max_target_p
Paul Duffine3dc6602021-04-14 09:50:43 +0100137 {
138 propertyName: "max_target_p",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100139 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100140 return properties.Max_target_p
141 },
142 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
143 command.FlagWithInput("--max-target-p ", path)
144 },
145 },
Paul Duffin46169772021-04-14 15:01:56 +0100146 // See HiddenAPIFlagFileProperties.Max_target_o_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100147 {
148 propertyName: "max_target_o_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100149 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100150 return properties.Max_target_o_low_priority
151 },
152 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
153 command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio")
154 },
155 },
Paul Duffin46169772021-04-14 15:01:56 +0100156 // See HiddenAPIFlagFileProperties.Blocked
Paul Duffine3dc6602021-04-14 09:50:43 +0100157 {
158 propertyName: "blocked",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100159 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100160 return properties.Blocked
161 },
162 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
163 command.FlagWithInput("--blocked ", path)
164 },
165 },
Paul Duffin46169772021-04-14 15:01:56 +0100166 // See HiddenAPIFlagFileProperties.Unsupported_packages
Paul Duffine3dc6602021-04-14 09:50:43 +0100167 {
168 propertyName: "unsupported_packages",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100169 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100170 return properties.Unsupported_packages
171 },
172 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
173 command.FlagWithInput("--unsupported ", path).Flag("--packages ")
174 },
175 },
Paul Duffin702210b2021-04-08 20:12:41 +0100176}
177
Paul Duffin46169772021-04-14 15:01:56 +0100178// hiddenAPIFlagFileInfo contains paths resolved from HiddenAPIFlagFileProperties
179type hiddenAPIFlagFileInfo struct {
Paul Duffine3dc6602021-04-14 09:50:43 +0100180 // categoryToPaths maps from the flag file category to the paths containing information for that
181 // category.
182 categoryToPaths map[*hiddenAPIFlagFileCategory]android.Paths
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100183}
Paul Duffin702210b2021-04-08 20:12:41 +0100184
Paul Duffin9b381ef2021-04-08 23:01:37 +0100185func (i *hiddenAPIFlagFileInfo) append(other hiddenAPIFlagFileInfo) {
186 for _, category := range hiddenAPIFlagFileCategories {
187 i.categoryToPaths[category] = append(i.categoryToPaths[category], other.categoryToPaths[category]...)
188 }
189}
190
191var hiddenAPIFlagFileInfoProvider = blueprint.NewProvider(hiddenAPIFlagFileInfo{})
192
Paul Duffin702210b2021-04-08 20:12:41 +0100193// ruleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from the
194// flags from all the modules, the stub flags, augmented with some additional configuration files.
195//
196// baseFlagsPath is the path to the flags file containing all the information from the stubs plus
197// an entry for every single member in the dex implementation jars of the individual modules. Every
198// signature in any of the other files MUST be included in this file.
199//
200// moduleSpecificFlagsPaths are the paths to the flags files generated by each module using
201// information from the baseFlagsPath as well as from annotations within the source.
202//
203// augmentationInfo is a struct containing paths to files that augment the information provided by
204// the moduleSpecificFlagsPaths.
205// ruleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from the
206// flags from all the modules, the stub flags, augmented with some additional configuration files.
207//
208// baseFlagsPath is the path to the flags file containing all the information from the stubs plus
209// an entry for every single member in the dex implementation jars of the individual modules. Every
210// signature in any of the other files MUST be included in this file.
211//
212// moduleSpecificFlagsPaths are the paths to the flags files generated by each module using
213// information from the baseFlagsPath as well as from annotations within the source.
214//
215// augmentationInfo is a struct containing paths to files that augment the information provided by
216// the moduleSpecificFlagsPaths.
Paul Duffin46169772021-04-14 15:01:56 +0100217func ruleToGenerateHiddenApiFlags(ctx android.BuilderContext, outputPath android.WritablePath, baseFlagsPath android.Path, moduleSpecificFlagsPaths android.Paths, augmentationInfo hiddenAPIFlagFileInfo) {
Paul Duffind3c15132021-04-21 22:12:35 +0100218 tempPath := tempPathForRestat(ctx, outputPath)
Paul Duffin702210b2021-04-08 20:12:41 +0100219 rule := android.NewRuleBuilder(pctx, ctx)
220 command := rule.Command().
221 BuiltTool("generate_hiddenapi_lists").
222 FlagWithInput("--csv ", baseFlagsPath).
223 Inputs(moduleSpecificFlagsPaths).
224 FlagWithOutput("--output ", tempPath)
225
Paul Duffine3dc6602021-04-14 09:50:43 +0100226 // Add the options for the different categories of flag files.
227 for _, category := range hiddenAPIFlagFileCategories {
228 paths := augmentationInfo.categoryToPaths[category]
229 for _, path := range paths {
230 category.commandMutator(command, path)
231 }
Paul Duffin702210b2021-04-08 20:12:41 +0100232 }
233
234 commitChangeForRestat(rule, tempPath, outputPath)
235
236 rule.Build("hiddenAPIFlagsFile", "hiddenapi flags")
237}