Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | ) |
| 20 | |
| 21 | // Contains support for processing hiddenAPI in a modular fashion. |
| 22 | |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 23 | // HiddenAPIAugmentationProperties contains paths to the files that can be used to augment the information |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 24 | // obtained from annotations within the source code in order to create the complete set of flags |
| 25 | // that should be applied to the dex implementation jars on the bootclasspath. |
| 26 | // |
| 27 | // Each property contains a list of paths. With the exception of the Unsupported_packages the paths |
| 28 | // of each property reference a plain text file that contains a java signature per line. The flags |
| 29 | // for each of those signatures will be updated in a property specific way. |
| 30 | // |
| 31 | // The Unsupported_packages property contains a list of paths, each of which is a plain text file |
| 32 | // with one Java package per line. All members of all classes within that package (but not nested |
| 33 | // packages) will be updated in a property specific way. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 34 | type HiddenAPIAugmentationProperties struct { |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 35 | // Marks each signature in the referenced files as being unsupported. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 36 | Unsupported []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 37 | |
| 38 | // Marks each signature in the referenced files as being unsupported because it has been removed. |
| 39 | // Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 40 | Removed []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 41 | |
| 42 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R |
| 43 | // and low priority. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 44 | Max_target_r_low_priority []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 45 | |
| 46 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 47 | Max_target_q []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 48 | |
| 49 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 50 | Max_target_p []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 51 | |
| 52 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O |
| 53 | // and low priority. Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 54 | Max_target_o_low_priority []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 55 | |
| 56 | // Marks each signature in the referenced files as being blocked. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 57 | Blocked []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 58 | |
| 59 | // Marks each signature in every package in the referenced files as being unsupported. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 60 | Unsupported_packages []string `android:"path"` |
| 61 | } |
| 62 | |
| 63 | func (p *HiddenAPIAugmentationProperties) hiddenAPIAugmentationInfo(ctx android.ModuleContext) hiddenAPIAugmentationInfo { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame^] | 64 | info := hiddenAPIAugmentationInfo{categoryToPaths: map[*hiddenAPIFlagFileCategory]android.Paths{}} |
| 65 | for _, category := range hiddenAPIFlagFileCategories { |
| 66 | paths := android.PathsForModuleSrc(ctx, category.propertyAccessor(p)) |
| 67 | info.categoryToPaths[category] = paths |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 68 | } |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame^] | 69 | return info |
| 70 | } |
| 71 | |
| 72 | type hiddenAPIFlagFileCategory struct { |
| 73 | // propertyName is the name of the property for this category. |
| 74 | propertyName string |
| 75 | |
| 76 | // propertyAccessor retrieves the value of the property for this category from the set of |
| 77 | // properties. |
| 78 | propertyAccessor func(properties *HiddenAPIAugmentationProperties) []string |
| 79 | |
| 80 | // commandMutator adds the appropriate command line options for this category to the supplied |
| 81 | // command |
| 82 | commandMutator func(command *android.RuleBuilderCommand, path android.Path) |
| 83 | } |
| 84 | |
| 85 | var hiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{ |
| 86 | // See HiddenAPIAugmentationProperties.Unsupported |
| 87 | { |
| 88 | propertyName: "unsupported", |
| 89 | propertyAccessor: func(properties *HiddenAPIAugmentationProperties) []string { |
| 90 | return properties.Unsupported |
| 91 | }, |
| 92 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 93 | command.FlagWithInput("--unsupported ", path) |
| 94 | }, |
| 95 | }, |
| 96 | // See HiddenAPIAugmentationProperties.Removed |
| 97 | { |
| 98 | propertyName: "removed", |
| 99 | propertyAccessor: func(properties *HiddenAPIAugmentationProperties) []string { |
| 100 | return properties.Removed |
| 101 | }, |
| 102 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 103 | command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed") |
| 104 | }, |
| 105 | }, |
| 106 | // See HiddenAPIAugmentationProperties.Max_target_r_low_priority |
| 107 | { |
| 108 | propertyName: "max_target_r_low_priority", |
| 109 | propertyAccessor: func(properties *HiddenAPIAugmentationProperties) []string { |
| 110 | return properties.Max_target_r_low_priority |
| 111 | }, |
| 112 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 113 | command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio") |
| 114 | }, |
| 115 | }, |
| 116 | // See HiddenAPIAugmentationProperties.Max_target_q |
| 117 | { |
| 118 | propertyName: "max_target_q", |
| 119 | propertyAccessor: func(properties *HiddenAPIAugmentationProperties) []string { |
| 120 | return properties.Max_target_q |
| 121 | }, |
| 122 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 123 | command.FlagWithInput("--max-target-q ", path) |
| 124 | }, |
| 125 | }, |
| 126 | // See HiddenAPIAugmentationProperties.Max_target_p |
| 127 | { |
| 128 | propertyName: "max_target_p", |
| 129 | propertyAccessor: func(properties *HiddenAPIAugmentationProperties) []string { |
| 130 | return properties.Max_target_p |
| 131 | }, |
| 132 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 133 | command.FlagWithInput("--max-target-p ", path) |
| 134 | }, |
| 135 | }, |
| 136 | // See HiddenAPIAugmentationProperties.Max_target_o_low_priority |
| 137 | { |
| 138 | propertyName: "max_target_o_low_priority", |
| 139 | propertyAccessor: func(properties *HiddenAPIAugmentationProperties) []string { |
| 140 | return properties.Max_target_o_low_priority |
| 141 | }, |
| 142 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 143 | command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio") |
| 144 | }, |
| 145 | }, |
| 146 | // See HiddenAPIAugmentationProperties.Blocked |
| 147 | { |
| 148 | propertyName: "blocked", |
| 149 | propertyAccessor: func(properties *HiddenAPIAugmentationProperties) []string { |
| 150 | return properties.Blocked |
| 151 | }, |
| 152 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 153 | command.FlagWithInput("--blocked ", path) |
| 154 | }, |
| 155 | }, |
| 156 | // See HiddenAPIAugmentationProperties.Unsupported_packages |
| 157 | { |
| 158 | propertyName: "unsupported_packages", |
| 159 | propertyAccessor: func(properties *HiddenAPIAugmentationProperties) []string { |
| 160 | return properties.Unsupported_packages |
| 161 | }, |
| 162 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 163 | command.FlagWithInput("--unsupported ", path).Flag("--packages ") |
| 164 | }, |
| 165 | }, |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // hiddenAPIAugmentationInfo contains paths resolved from HiddenAPIAugmentationProperties |
| 169 | type hiddenAPIAugmentationInfo struct { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame^] | 170 | // categoryToPaths maps from the flag file category to the paths containing information for that |
| 171 | // category. |
| 172 | categoryToPaths map[*hiddenAPIFlagFileCategory]android.Paths |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 173 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 174 | |
| 175 | // ruleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from the |
| 176 | // flags from all the modules, the stub flags, augmented with some additional configuration files. |
| 177 | // |
| 178 | // baseFlagsPath is the path to the flags file containing all the information from the stubs plus |
| 179 | // an entry for every single member in the dex implementation jars of the individual modules. Every |
| 180 | // signature in any of the other files MUST be included in this file. |
| 181 | // |
| 182 | // moduleSpecificFlagsPaths are the paths to the flags files generated by each module using |
| 183 | // information from the baseFlagsPath as well as from annotations within the source. |
| 184 | // |
| 185 | // augmentationInfo is a struct containing paths to files that augment the information provided by |
| 186 | // the moduleSpecificFlagsPaths. |
| 187 | // ruleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from the |
| 188 | // flags from all the modules, the stub flags, augmented with some additional configuration files. |
| 189 | // |
| 190 | // baseFlagsPath is the path to the flags file containing all the information from the stubs plus |
| 191 | // an entry for every single member in the dex implementation jars of the individual modules. Every |
| 192 | // signature in any of the other files MUST be included in this file. |
| 193 | // |
| 194 | // moduleSpecificFlagsPaths are the paths to the flags files generated by each module using |
| 195 | // information from the baseFlagsPath as well as from annotations within the source. |
| 196 | // |
| 197 | // augmentationInfo is a struct containing paths to files that augment the information provided by |
| 198 | // the moduleSpecificFlagsPaths. |
| 199 | func ruleToGenerateHiddenApiFlags(ctx android.BuilderContext, outputPath android.WritablePath, baseFlagsPath android.Path, moduleSpecificFlagsPaths android.Paths, augmentationInfo hiddenAPIAugmentationInfo) { |
| 200 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") |
| 201 | rule := android.NewRuleBuilder(pctx, ctx) |
| 202 | command := rule.Command(). |
| 203 | BuiltTool("generate_hiddenapi_lists"). |
| 204 | FlagWithInput("--csv ", baseFlagsPath). |
| 205 | Inputs(moduleSpecificFlagsPaths). |
| 206 | FlagWithOutput("--output ", tempPath) |
| 207 | |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame^] | 208 | // Add the options for the different categories of flag files. |
| 209 | for _, category := range hiddenAPIFlagFileCategories { |
| 210 | paths := augmentationInfo.categoryToPaths[category] |
| 211 | for _, path := range paths { |
| 212 | category.commandMutator(command, path) |
| 213 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | commitChangeForRestat(rule, tempPath, outputPath) |
| 217 | |
| 218 | rule.Build("hiddenAPIFlagsFile", "hiddenapi flags") |
| 219 | } |