blob: d43e276ac323362bb1e5560ddfea67d424bffe3d [file] [log] [blame]
Colin Crossf24a22a2019-01-31 14:12:44 -08001// Copyright 2019 Google Inc. All rights reserved.
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
21func init() {
Colin Cross76e3e1f2019-02-07 08:57:26 -080022 android.RegisterPreSingletonType("pre-hiddenapi", hiddenAPIPreSingletonFactory)
Colin Crossf24a22a2019-01-31 14:12:44 -080023 android.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
24}
25
26type hiddenAPISingletonPathsStruct struct {
27 stubFlags android.OutputPath
28 flags android.OutputPath
29 metadata android.OutputPath
30}
31
32var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
33
34// hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
35// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
36// yet been created.
37func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
38 return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
39 return hiddenAPISingletonPathsStruct{
40 stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"),
41 flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"),
42 metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-greylist.csv"),
43 }
44 }).(hiddenAPISingletonPathsStruct)
45}
46
Colin Cross76e3e1f2019-02-07 08:57:26 -080047func hiddenAPIPreSingletonFactory() android.Singleton {
48 return hiddenAPIPreSingleton{}
49}
50
51type hiddenAPIPreSingleton struct{}
52
53// hiddenAPI pre-singleton rules to ensure paths are always generated before
54// makevars
55func (hiddenAPIPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
56 hiddenAPISingletonPaths(ctx)
57}
58
Colin Crossf24a22a2019-01-31 14:12:44 -080059func hiddenAPISingletonFactory() android.Singleton {
60 return hiddenAPISingleton{}
61}
62
63type hiddenAPISingleton struct{}
64
65// hiddenAPI singleton rules
66func (hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
67 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
68 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
69 return
70 }
71
72 stubFlagsRule(ctx)
73
74 // These rules depend on files located in frameworks/base, skip them if running in a tree that doesn't have them.
75 if ctx.Config().FrameworksBaseDirExists(ctx) {
76 flagsRule(ctx)
77 metadataRule(ctx)
78 } else {
79 emptyFlagsRule(ctx)
80 }
81}
82
83// stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image
84// modules.
85func stubFlagsRule(ctx android.SingletonContext) {
86 // Public API stubs
87 publicStubModules := []string{
88 "android_stubs_current",
89 "android.test.base.stubs",
90 }
91
92 // System API stubs
93 systemStubModules := []string{
94 "android_system_stubs_current",
95 }
96
97 // Test API stubs
98 testStubModules := []string{
99 "android_test_stubs_current",
100 }
101
102 // Core Platform API stubs
103 corePlatformStubModules := []string{
104 "core.platform.api.stubs",
105 }
106
107 // Allow products to define their own stubs for custom product jars that apps can use.
108 publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
109 systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
110 testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
111
112 publicStubPaths := make(android.Paths, len(publicStubModules))
113 systemStubPaths := make(android.Paths, len(systemStubModules))
114 testStubPaths := make(android.Paths, len(testStubModules))
115 corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules))
116
117 moduleListToPathList := map[*[]string]android.Paths{
118 &publicStubModules: publicStubPaths,
119 &systemStubModules: systemStubPaths,
120 &testStubModules: testStubPaths,
121 &corePlatformStubModules: corePlatformStubPaths,
122 }
123
124 var bootDexJars android.Paths
125
126 ctx.VisitAllModules(func(module android.Module) {
127 // Collect dex jar paths for the modules listed above.
128 if j, ok := module.(Dependency); ok {
129 name := ctx.ModuleName(module)
130 for moduleList, pathList := range moduleListToPathList {
131 if i := android.IndexList(name, *moduleList); i != -1 {
132 pathList[i] = j.DexJar()
133 }
134 }
135 }
136
137 // Collect dex jar paths for modules that had hiddenapi encode called on them.
138 if h, ok := module.(hiddenAPIIntf); ok {
139 if jar := h.bootDexJar(); jar != nil {
140 bootDexJars = append(bootDexJars, jar)
141 }
142 }
143 })
144
145 var missingDeps []string
146 // Ensure all modules were converted to paths
147 for moduleList, pathList := range moduleListToPathList {
148 for i := range pathList {
149 if pathList[i] == nil {
150 if ctx.Config().AllowMissingDependencies() {
151 missingDeps = append(missingDeps, (*moduleList)[i])
152 pathList[i] = android.PathForOutput(ctx, "missing")
153 } else {
154 ctx.Errorf("failed to find dex jar path for module %q",
155 (*moduleList)[i])
156 }
157 }
158 }
159 }
160
161 // Singleton rule which applies hiddenapi on all boot class path dex files.
162 rule := android.NewRuleBuilder()
163
164 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
165 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
166
167 rule.MissingDeps(missingDeps)
168
169 rule.Command().
170 Tool(pctx.HostBinToolPath(ctx, "hiddenapi").String()).
171 Text("list").
172 FlagForEachInput("--boot-dex=", bootDexJars.Strings()).
173 FlagWithInputList("--public-stub-classpath=", publicStubPaths.Strings(), ":").
174 FlagWithInputList("--public-stub-classpath=", systemStubPaths.Strings(), ":").
175 FlagWithInputList("--public-stub-classpath=", testStubPaths.Strings(), ":").
176 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths.Strings(), ":").
177 FlagWithOutput("--out-api-flags=", tempPath.String())
178
179 commitChangeForRestat(rule, tempPath, outputPath)
180
181 rule.Build(pctx, ctx, "hiddenAPIStubFlagsFile", "hiddenapi stub flags")
182}
183
184// flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and
185// the greylists.
186func flagsRule(ctx android.SingletonContext) {
187 var flagsCSV android.Paths
188
189 var greylistIgnoreConflicts android.Path
190
191 ctx.VisitAllModules(func(module android.Module) {
192 if h, ok := module.(hiddenAPIIntf); ok {
193 if csv := h.flagsCSV(); csv != nil {
194 flagsCSV = append(flagsCSV, csv)
195 }
196 } else if ds, ok := module.(*Droidstubs); ok && ctx.ModuleName(module) == "hiddenapi-lists-docs" {
197 greylistIgnoreConflicts = ds.removedDexApiFile
198 }
199 })
200
201 if greylistIgnoreConflicts == nil {
202 ctx.Errorf("failed to find removed_dex_api_filename from hiddenapi-lists-docs module")
203 return
204 }
205
206 rule := android.NewRuleBuilder()
207
208 outputPath := hiddenAPISingletonPaths(ctx).flags
209 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
210
211 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
212
213 rule.Command().
214 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py").String()).
215 FlagWithInput("--csv ", stubFlags.String()).
216 Inputs(flagsCSV.Strings()).
217 FlagWithInput("--greylist ",
218 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt").String()).
219 FlagWithInput("--greylist-ignore-conflicts ",
220 greylistIgnoreConflicts.String()).
221 FlagWithInput("--greylist-max-p ",
222 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt").String()).
223 FlagWithInput("--greylist-max-o-ignore-conflicts ",
224 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt").String()).
225 FlagWithInput("--blacklist ",
226 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt").String()).
227 FlagWithOutput("--output ", tempPath.String())
228
229 commitChangeForRestat(rule, tempPath, outputPath)
230
231 rule.Build(pctx, ctx, "hiddenAPIFlagsFile", "hiddenapi flags")
232}
233
234// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
235// have a partial manifest without frameworks/base but still need to build a boot image.
236func emptyFlagsRule(ctx android.SingletonContext) {
237 rule := android.NewRuleBuilder()
238
239 outputPath := hiddenAPISingletonPaths(ctx).flags
240
241 rule.Command().Text("rm").Flag("-f").Output(outputPath.String())
242 rule.Command().Text("touch").Output(outputPath.String())
243
244 rule.Build(pctx, ctx, "emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
245}
246
247// metadataRule creates a rule to build hiddenapi-greylist.csv out of the metadata.csv files generated for boot image
248// modules.
249func metadataRule(ctx android.SingletonContext) {
250 var metadataCSV android.Paths
251
252 ctx.VisitAllModules(func(module android.Module) {
253 if h, ok := module.(hiddenAPIIntf); ok {
254 if csv := h.metadataCSV(); csv != nil {
255 metadataCSV = append(metadataCSV, csv)
256 }
257 }
258 })
259
260 rule := android.NewRuleBuilder()
261
262 outputPath := hiddenAPISingletonPaths(ctx).metadata
263
264 rule.Command().
265 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/merge_csv.py").String()).
266 Inputs(metadataCSV.Strings()).
267 Text(">").
268 Output(outputPath.String())
269
270 rule.Build(pctx, ctx, "hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
271}
272
273// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
274// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
275// the rule.
276func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
277 rule.Restat()
278 rule.Temporary(tempPath.String())
279 rule.Command().
280 Text("(").
281 Text("if").
282 Text("cmp -s").Input(tempPath.String()).Output(outputPath.String()).Text(";").
283 Text("then").
284 Text("rm").Input(tempPath.String()).Text(";").
285 Text("else").
286 Text("mv").Input(tempPath.String()).Output(outputPath.String()).Text(";").
287 Text("fi").
288 Text(")")
289}
290
291func init() {
292 android.RegisterMakeVarsProvider(pctx, hiddenAPIMakeVars)
293}
294
295// Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
296// Both paths are used to call dist-for-goals.
297func hiddenAPIMakeVars(ctx android.MakeVarsContext) {
298 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
299 singletonPaths := ctx.Config().Get(hiddenAPISingletonPathsKey).(hiddenAPISingletonPathsStruct)
300 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", singletonPaths.flags.String())
301 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", singletonPaths.metadata.String())
302 }
303}