blob: 2814e741055c09c8c2dfbebcefb2f432e70d8ce2 [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 Duffin702210b2021-04-08 20:12:41 +010023// HiddenAPIAugmentationProperties contains paths to the files that can be used to augment the information
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010024// 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 Duffin702210b2021-04-08 20:12:41 +010034type HiddenAPIAugmentationProperties 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
63func (p *HiddenAPIAugmentationProperties) hiddenAPIAugmentationInfo(ctx android.ModuleContext) hiddenAPIAugmentationInfo {
Paul Duffine3dc6602021-04-14 09:50:43 +010064 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 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.
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
85var 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 Duffin702210b2021-04-08 20:12:41 +0100166}
167
168// hiddenAPIAugmentationInfo contains paths resolved from HiddenAPIAugmentationProperties
169type hiddenAPIAugmentationInfo 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.
199func 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 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}