blob: 7cf082b6ac48aa52e0c4852f77fd536c4d19d074 [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"
19)
20
21// Contains support for processing hiddenAPI in a modular fashion.
22
Paul Duffin46169772021-04-14 15:01:56 +010023// HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the
24// information obtained from annotations within the source code in order to create the complete set
25// of flags that should be applied to the dex implementation jars on the bootclasspath.
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010026//
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 Duffin46169772021-04-14 15:01:56 +010034type HiddenAPIFlagFileProperties struct {
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010035 // Marks each signature in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +010036 Unsupported []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010037
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 Duffin702210b2021-04-08 20:12:41 +010040 Removed []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010041
42 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R
43 // and low priority.
Paul Duffin702210b2021-04-08 20:12:41 +010044 Max_target_r_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010045
46 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q.
Paul Duffin702210b2021-04-08 20:12:41 +010047 Max_target_q []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010048
49 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P.
Paul Duffin702210b2021-04-08 20:12:41 +010050 Max_target_p []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 <= O
53 // and low priority. Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +010054 Max_target_o_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010055
56 // Marks each signature in the referenced files as being blocked.
Paul Duffin702210b2021-04-08 20:12:41 +010057 Blocked []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010058
59 // Marks each signature in every package in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +010060 Unsupported_packages []string `android:"path"`
61}
62
Paul Duffin46169772021-04-14 15:01:56 +010063func (p *HiddenAPIFlagFileProperties) hiddenAPIFlagFileInfo(ctx android.ModuleContext) hiddenAPIFlagFileInfo {
64 info := hiddenAPIFlagFileInfo{categoryToPaths: map[*hiddenAPIFlagFileCategory]android.Paths{}}
Paul Duffine3dc6602021-04-14 09:50:43 +010065 for _, category := range hiddenAPIFlagFileCategories {
66 paths := android.PathsForModuleSrc(ctx, category.propertyAccessor(p))
67 info.categoryToPaths[category] = paths
Paul Duffin702210b2021-04-08 20:12:41 +010068 }
Paul Duffine3dc6602021-04-14 09:50:43 +010069 return info
70}
71
72type 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.
Paul Duffin46169772021-04-14 15:01:56 +010078 propertyAccessor func(properties *HiddenAPIFlagFileProperties) []string
Paul Duffine3dc6602021-04-14 09:50:43 +010079
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
85var hiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{
Paul Duffin46169772021-04-14 15:01:56 +010086 // See HiddenAPIFlagFileProperties.Unsupported
Paul Duffine3dc6602021-04-14 09:50:43 +010087 {
88 propertyName: "unsupported",
Paul Duffin46169772021-04-14 15:01:56 +010089 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +010090 return properties.Unsupported
91 },
92 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
93 command.FlagWithInput("--unsupported ", path)
94 },
95 },
Paul Duffin46169772021-04-14 15:01:56 +010096 // See HiddenAPIFlagFileProperties.Removed
Paul Duffine3dc6602021-04-14 09:50:43 +010097 {
98 propertyName: "removed",
Paul Duffin46169772021-04-14 15:01:56 +010099 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100100 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 },
Paul Duffin46169772021-04-14 15:01:56 +0100106 // See HiddenAPIFlagFileProperties.Max_target_r_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100107 {
108 propertyName: "max_target_r_low_priority",
Paul Duffin46169772021-04-14 15:01:56 +0100109 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100110 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 },
Paul Duffin46169772021-04-14 15:01:56 +0100116 // See HiddenAPIFlagFileProperties.Max_target_q
Paul Duffine3dc6602021-04-14 09:50:43 +0100117 {
118 propertyName: "max_target_q",
Paul Duffin46169772021-04-14 15:01:56 +0100119 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100120 return properties.Max_target_q
121 },
122 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
123 command.FlagWithInput("--max-target-q ", path)
124 },
125 },
Paul Duffin46169772021-04-14 15:01:56 +0100126 // See HiddenAPIFlagFileProperties.Max_target_p
Paul Duffine3dc6602021-04-14 09:50:43 +0100127 {
128 propertyName: "max_target_p",
Paul Duffin46169772021-04-14 15:01:56 +0100129 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100130 return properties.Max_target_p
131 },
132 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
133 command.FlagWithInput("--max-target-p ", path)
134 },
135 },
Paul Duffin46169772021-04-14 15:01:56 +0100136 // See HiddenAPIFlagFileProperties.Max_target_o_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100137 {
138 propertyName: "max_target_o_low_priority",
Paul Duffin46169772021-04-14 15:01:56 +0100139 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100140 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 },
Paul Duffin46169772021-04-14 15:01:56 +0100146 // See HiddenAPIFlagFileProperties.Blocked
Paul Duffine3dc6602021-04-14 09:50:43 +0100147 {
148 propertyName: "blocked",
Paul Duffin46169772021-04-14 15:01:56 +0100149 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100150 return properties.Blocked
151 },
152 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
153 command.FlagWithInput("--blocked ", path)
154 },
155 },
Paul Duffin46169772021-04-14 15:01:56 +0100156 // See HiddenAPIFlagFileProperties.Unsupported_packages
Paul Duffine3dc6602021-04-14 09:50:43 +0100157 {
158 propertyName: "unsupported_packages",
Paul Duffin46169772021-04-14 15:01:56 +0100159 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100160 return properties.Unsupported_packages
161 },
162 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
163 command.FlagWithInput("--unsupported ", path).Flag("--packages ")
164 },
165 },
Paul Duffin702210b2021-04-08 20:12:41 +0100166}
167
Paul Duffin46169772021-04-14 15:01:56 +0100168// hiddenAPIFlagFileInfo contains paths resolved from HiddenAPIFlagFileProperties
169type hiddenAPIFlagFileInfo struct {
Paul Duffine3dc6602021-04-14 09:50:43 +0100170 // categoryToPaths maps from the flag file category to the paths containing information for that
171 // category.
172 categoryToPaths map[*hiddenAPIFlagFileCategory]android.Paths
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100173}
Paul Duffin702210b2021-04-08 20:12:41 +0100174
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.
Paul Duffin46169772021-04-14 15:01:56 +0100199func ruleToGenerateHiddenApiFlags(ctx android.BuilderContext, outputPath android.WritablePath, baseFlagsPath android.Path, moduleSpecificFlagsPaths android.Paths, augmentationInfo hiddenAPIFlagFileInfo) {
Paul Duffin702210b2021-04-08 20:12:41 +0100200 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 Duffine3dc6602021-04-14 09:50:43 +0100208 // 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 Duffin702210b2021-04-08 20:12:41 +0100214 }
215
216 commitChangeForRestat(rule, tempPath, outputPath)
217
218 rule.Build("hiddenAPIFlagsFile", "hiddenapi flags")
219}