blob: e5dba33e800ac52da68dbacbb0d82d79abea333c [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 Duffin46169772021-04-14 15:01:56 +010024// HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the
25// information obtained from annotations within the source code in order to create the complete set
26// of flags that should be applied to the dex implementation jars on the bootclasspath.
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010027//
28// Each property contains a list of paths. With the exception of the Unsupported_packages the paths
29// of each property reference a plain text file that contains a java signature per line. The flags
30// for each of those signatures will be updated in a property specific way.
31//
32// The Unsupported_packages property contains a list of paths, each of which is a plain text file
33// with one Java package per line. All members of all classes within that package (but not nested
34// packages) will be updated in a property specific way.
Paul Duffin46169772021-04-14 15:01:56 +010035type HiddenAPIFlagFileProperties struct {
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010036 // Marks each signature in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +010037 Unsupported []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010038
39 // Marks each signature in the referenced files as being unsupported because it has been removed.
40 // Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +010041 Removed []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010042
43 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R
44 // and low priority.
Paul Duffin702210b2021-04-08 20:12:41 +010045 Max_target_r_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010046
47 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q.
Paul Duffin702210b2021-04-08 20:12:41 +010048 Max_target_q []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010049
50 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P.
Paul Duffin702210b2021-04-08 20:12:41 +010051 Max_target_p []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010052
53 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O
54 // and low priority. Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +010055 Max_target_o_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010056
57 // Marks each signature in the referenced files as being blocked.
Paul Duffin702210b2021-04-08 20:12:41 +010058 Blocked []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010059
60 // Marks each signature in every package in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +010061 Unsupported_packages []string `android:"path"`
62}
63
Paul Duffin46169772021-04-14 15:01:56 +010064func (p *HiddenAPIFlagFileProperties) hiddenAPIFlagFileInfo(ctx android.ModuleContext) hiddenAPIFlagFileInfo {
65 info := hiddenAPIFlagFileInfo{categoryToPaths: map[*hiddenAPIFlagFileCategory]android.Paths{}}
Paul Duffine3dc6602021-04-14 09:50:43 +010066 for _, category := range hiddenAPIFlagFileCategories {
67 paths := android.PathsForModuleSrc(ctx, category.propertyAccessor(p))
68 info.categoryToPaths[category] = paths
Paul Duffin702210b2021-04-08 20:12:41 +010069 }
Paul Duffine3dc6602021-04-14 09:50:43 +010070 return info
71}
72
73type hiddenAPIFlagFileCategory struct {
74 // propertyName is the name of the property for this category.
75 propertyName string
76
77 // propertyAccessor retrieves the value of the property for this category from the set of
78 // properties.
Paul Duffin46169772021-04-14 15:01:56 +010079 propertyAccessor func(properties *HiddenAPIFlagFileProperties) []string
Paul Duffine3dc6602021-04-14 09:50:43 +010080
81 // commandMutator adds the appropriate command line options for this category to the supplied
82 // command
83 commandMutator func(command *android.RuleBuilderCommand, path android.Path)
84}
85
86var hiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{
Paul Duffin46169772021-04-14 15:01:56 +010087 // See HiddenAPIFlagFileProperties.Unsupported
Paul Duffine3dc6602021-04-14 09:50:43 +010088 {
89 propertyName: "unsupported",
Paul Duffin46169772021-04-14 15:01:56 +010090 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +010091 return properties.Unsupported
92 },
93 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
94 command.FlagWithInput("--unsupported ", path)
95 },
96 },
Paul Duffin46169772021-04-14 15:01:56 +010097 // See HiddenAPIFlagFileProperties.Removed
Paul Duffine3dc6602021-04-14 09:50:43 +010098 {
99 propertyName: "removed",
Paul Duffin46169772021-04-14 15:01:56 +0100100 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100101 return properties.Removed
102 },
103 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
104 command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed")
105 },
106 },
Paul Duffin46169772021-04-14 15:01:56 +0100107 // See HiddenAPIFlagFileProperties.Max_target_r_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100108 {
109 propertyName: "max_target_r_low_priority",
Paul Duffin46169772021-04-14 15:01:56 +0100110 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100111 return properties.Max_target_r_low_priority
112 },
113 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
114 command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio")
115 },
116 },
Paul Duffin46169772021-04-14 15:01:56 +0100117 // See HiddenAPIFlagFileProperties.Max_target_q
Paul Duffine3dc6602021-04-14 09:50:43 +0100118 {
119 propertyName: "max_target_q",
Paul Duffin46169772021-04-14 15:01:56 +0100120 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100121 return properties.Max_target_q
122 },
123 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
124 command.FlagWithInput("--max-target-q ", path)
125 },
126 },
Paul Duffin46169772021-04-14 15:01:56 +0100127 // See HiddenAPIFlagFileProperties.Max_target_p
Paul Duffine3dc6602021-04-14 09:50:43 +0100128 {
129 propertyName: "max_target_p",
Paul Duffin46169772021-04-14 15:01:56 +0100130 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100131 return properties.Max_target_p
132 },
133 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
134 command.FlagWithInput("--max-target-p ", path)
135 },
136 },
Paul Duffin46169772021-04-14 15:01:56 +0100137 // See HiddenAPIFlagFileProperties.Max_target_o_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100138 {
139 propertyName: "max_target_o_low_priority",
Paul Duffin46169772021-04-14 15:01:56 +0100140 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100141 return properties.Max_target_o_low_priority
142 },
143 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
144 command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio")
145 },
146 },
Paul Duffin46169772021-04-14 15:01:56 +0100147 // See HiddenAPIFlagFileProperties.Blocked
Paul Duffine3dc6602021-04-14 09:50:43 +0100148 {
149 propertyName: "blocked",
Paul Duffin46169772021-04-14 15:01:56 +0100150 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100151 return properties.Blocked
152 },
153 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
154 command.FlagWithInput("--blocked ", path)
155 },
156 },
Paul Duffin46169772021-04-14 15:01:56 +0100157 // See HiddenAPIFlagFileProperties.Unsupported_packages
Paul Duffine3dc6602021-04-14 09:50:43 +0100158 {
159 propertyName: "unsupported_packages",
Paul Duffin46169772021-04-14 15:01:56 +0100160 propertyAccessor: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100161 return properties.Unsupported_packages
162 },
163 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
164 command.FlagWithInput("--unsupported ", path).Flag("--packages ")
165 },
166 },
Paul Duffin702210b2021-04-08 20:12:41 +0100167}
168
Paul Duffin46169772021-04-14 15:01:56 +0100169// hiddenAPIFlagFileInfo contains paths resolved from HiddenAPIFlagFileProperties
170type hiddenAPIFlagFileInfo struct {
Paul Duffine3dc6602021-04-14 09:50:43 +0100171 // categoryToPaths maps from the flag file category to the paths containing information for that
172 // category.
173 categoryToPaths map[*hiddenAPIFlagFileCategory]android.Paths
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100174}
Paul Duffin702210b2021-04-08 20:12:41 +0100175
Paul Duffin9b381ef2021-04-08 23:01:37 +0100176func (i *hiddenAPIFlagFileInfo) append(other hiddenAPIFlagFileInfo) {
177 for _, category := range hiddenAPIFlagFileCategories {
178 i.categoryToPaths[category] = append(i.categoryToPaths[category], other.categoryToPaths[category]...)
179 }
180}
181
182var hiddenAPIFlagFileInfoProvider = blueprint.NewProvider(hiddenAPIFlagFileInfo{})
183
Paul Duffin702210b2021-04-08 20:12:41 +0100184// ruleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from the
185// flags from all the modules, the stub flags, augmented with some additional configuration files.
186//
187// baseFlagsPath is the path to the flags file containing all the information from the stubs plus
188// an entry for every single member in the dex implementation jars of the individual modules. Every
189// signature in any of the other files MUST be included in this file.
190//
191// moduleSpecificFlagsPaths are the paths to the flags files generated by each module using
192// information from the baseFlagsPath as well as from annotations within the source.
193//
194// augmentationInfo is a struct containing paths to files that augment the information provided by
195// the moduleSpecificFlagsPaths.
196// ruleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from the
197// flags from all the modules, the stub flags, augmented with some additional configuration files.
198//
199// baseFlagsPath is the path to the flags file containing all the information from the stubs plus
200// an entry for every single member in the dex implementation jars of the individual modules. Every
201// signature in any of the other files MUST be included in this file.
202//
203// moduleSpecificFlagsPaths are the paths to the flags files generated by each module using
204// information from the baseFlagsPath as well as from annotations within the source.
205//
206// augmentationInfo is a struct containing paths to files that augment the information provided by
207// the moduleSpecificFlagsPaths.
Paul Duffin46169772021-04-14 15:01:56 +0100208func ruleToGenerateHiddenApiFlags(ctx android.BuilderContext, outputPath android.WritablePath, baseFlagsPath android.Path, moduleSpecificFlagsPaths android.Paths, augmentationInfo hiddenAPIFlagFileInfo) {
Paul Duffin702210b2021-04-08 20:12:41 +0100209 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
210 rule := android.NewRuleBuilder(pctx, ctx)
211 command := rule.Command().
212 BuiltTool("generate_hiddenapi_lists").
213 FlagWithInput("--csv ", baseFlagsPath).
214 Inputs(moduleSpecificFlagsPaths).
215 FlagWithOutput("--output ", tempPath)
216
Paul Duffine3dc6602021-04-14 09:50:43 +0100217 // Add the options for the different categories of flag files.
218 for _, category := range hiddenAPIFlagFileCategories {
219 paths := augmentationInfo.categoryToPaths[category]
220 for _, path := range paths {
221 category.commandMutator(command, path)
222 }
Paul Duffin702210b2021-04-08 20:12:41 +0100223 }
224
225 commitChangeForRestat(rule, tempPath, outputPath)
226
227 rule.Build("hiddenAPIFlagsFile", "hiddenapi flags")
228}