blob: 23f6cb0ee59d8b91e47c1ced3e6046814a127d62 [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() {
22 android.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
23}
24
25type hiddenAPISingletonPathsStruct struct {
26 stubFlags android.OutputPath
27 flags android.OutputPath
28 metadata android.OutputPath
29}
30
31var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
32
33// hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
34// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
35// yet been created.
36func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
37 return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
38 return hiddenAPISingletonPathsStruct{
39 stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"),
40 flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"),
41 metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-greylist.csv"),
42 }
43 }).(hiddenAPISingletonPathsStruct)
44}
45
Colin Crossf24a22a2019-01-31 14:12:44 -080046func hiddenAPISingletonFactory() android.Singleton {
Colin Crossed023ec2019-02-19 12:38:45 -080047 return &hiddenAPISingleton{}
Colin Crossf24a22a2019-01-31 14:12:44 -080048}
49
Colin Crossed023ec2019-02-19 12:38:45 -080050type hiddenAPISingleton struct {
51 flags, metadata android.Path
52}
Colin Crossf24a22a2019-01-31 14:12:44 -080053
54// hiddenAPI singleton rules
Colin Crossed023ec2019-02-19 12:38:45 -080055func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Crossf24a22a2019-01-31 14:12:44 -080056 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
57 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
58 return
59 }
60
61 stubFlagsRule(ctx)
62
63 // These rules depend on files located in frameworks/base, skip them if running in a tree that doesn't have them.
64 if ctx.Config().FrameworksBaseDirExists(ctx) {
Colin Crossed023ec2019-02-19 12:38:45 -080065 h.flags = flagsRule(ctx)
66 h.metadata = metadataRule(ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -080067 } else {
Colin Crossed023ec2019-02-19 12:38:45 -080068 h.flags = emptyFlagsRule(ctx)
69 }
70}
71
72// Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
73// Both paths are used to call dist-for-goals.
74func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) {
75 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
76 return
77 }
78
79 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String())
80
81 if h.metadata != nil {
82 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String())
Colin Crossf24a22a2019-01-31 14:12:44 -080083 }
84}
85
86// stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image
87// modules.
88func stubFlagsRule(ctx android.SingletonContext) {
89 // Public API stubs
90 publicStubModules := []string{
91 "android_stubs_current",
92 "android.test.base.stubs",
93 }
94
95 // System API stubs
96 systemStubModules := []string{
97 "android_system_stubs_current",
98 }
99
100 // Test API stubs
101 testStubModules := []string{
102 "android_test_stubs_current",
103 }
104
105 // Core Platform API stubs
106 corePlatformStubModules := []string{
107 "core.platform.api.stubs",
108 }
109
110 // Allow products to define their own stubs for custom product jars that apps can use.
111 publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
112 systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
113 testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
Allen Hairde816cf2019-02-25 16:37:42 -0800114 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") {
115 publicStubModules = append(publicStubModules, "jacoco-stubs")
116 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800117
118 publicStubPaths := make(android.Paths, len(publicStubModules))
119 systemStubPaths := make(android.Paths, len(systemStubModules))
120 testStubPaths := make(android.Paths, len(testStubModules))
121 corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules))
122
123 moduleListToPathList := map[*[]string]android.Paths{
124 &publicStubModules: publicStubPaths,
125 &systemStubModules: systemStubPaths,
126 &testStubModules: testStubPaths,
127 &corePlatformStubModules: corePlatformStubPaths,
128 }
129
130 var bootDexJars android.Paths
131
132 ctx.VisitAllModules(func(module android.Module) {
133 // Collect dex jar paths for the modules listed above.
134 if j, ok := module.(Dependency); ok {
135 name := ctx.ModuleName(module)
136 for moduleList, pathList := range moduleListToPathList {
137 if i := android.IndexList(name, *moduleList); i != -1 {
138 pathList[i] = j.DexJar()
139 }
140 }
141 }
142
143 // Collect dex jar paths for modules that had hiddenapi encode called on them.
144 if h, ok := module.(hiddenAPIIntf); ok {
145 if jar := h.bootDexJar(); jar != nil {
146 bootDexJars = append(bootDexJars, jar)
147 }
148 }
149 })
150
151 var missingDeps []string
152 // Ensure all modules were converted to paths
153 for moduleList, pathList := range moduleListToPathList {
154 for i := range pathList {
155 if pathList[i] == nil {
156 if ctx.Config().AllowMissingDependencies() {
157 missingDeps = append(missingDeps, (*moduleList)[i])
158 pathList[i] = android.PathForOutput(ctx, "missing")
159 } else {
160 ctx.Errorf("failed to find dex jar path for module %q",
161 (*moduleList)[i])
162 }
163 }
164 }
165 }
166
167 // Singleton rule which applies hiddenapi on all boot class path dex files.
168 rule := android.NewRuleBuilder()
169
170 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
171 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
172
173 rule.MissingDeps(missingDeps)
174
175 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800176 Tool(pctx.HostBinToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800177 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800178 FlagForEachInput("--boot-dex=", bootDexJars).
179 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
180 FlagWithInputList("--public-stub-classpath=", systemStubPaths, ":").
181 FlagWithInputList("--public-stub-classpath=", testStubPaths, ":").
182 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
183 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800184
185 commitChangeForRestat(rule, tempPath, outputPath)
186
187 rule.Build(pctx, ctx, "hiddenAPIStubFlagsFile", "hiddenapi stub flags")
188}
189
190// flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and
191// the greylists.
Colin Crossed023ec2019-02-19 12:38:45 -0800192func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800193 var flagsCSV android.Paths
194
195 var greylistIgnoreConflicts android.Path
196
197 ctx.VisitAllModules(func(module android.Module) {
198 if h, ok := module.(hiddenAPIIntf); ok {
199 if csv := h.flagsCSV(); csv != nil {
200 flagsCSV = append(flagsCSV, csv)
201 }
202 } else if ds, ok := module.(*Droidstubs); ok && ctx.ModuleName(module) == "hiddenapi-lists-docs" {
203 greylistIgnoreConflicts = ds.removedDexApiFile
204 }
205 })
206
207 if greylistIgnoreConflicts == nil {
208 ctx.Errorf("failed to find removed_dex_api_filename from hiddenapi-lists-docs module")
Colin Crossed023ec2019-02-19 12:38:45 -0800209 return nil
Colin Crossf24a22a2019-01-31 14:12:44 -0800210 }
211
212 rule := android.NewRuleBuilder()
213
214 outputPath := hiddenAPISingletonPaths(ctx).flags
215 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
216
217 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
218
219 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800220 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")).
221 FlagWithInput("--csv ", stubFlags).
222 Inputs(flagsCSV).
Colin Crossf24a22a2019-01-31 14:12:44 -0800223 FlagWithInput("--greylist ",
Colin Cross69f59a32019-02-15 10:39:37 -0800224 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800225 FlagWithInput("--greylist-ignore-conflicts ",
Colin Cross69f59a32019-02-15 10:39:37 -0800226 greylistIgnoreConflicts).
Colin Crossf24a22a2019-01-31 14:12:44 -0800227 FlagWithInput("--greylist-max-p ",
Colin Cross69f59a32019-02-15 10:39:37 -0800228 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800229 FlagWithInput("--greylist-max-o-ignore-conflicts ",
Colin Cross69f59a32019-02-15 10:39:37 -0800230 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800231 FlagWithInput("--blacklist ",
Colin Cross69f59a32019-02-15 10:39:37 -0800232 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt")).
233 FlagWithOutput("--output ", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800234
235 commitChangeForRestat(rule, tempPath, outputPath)
236
237 rule.Build(pctx, ctx, "hiddenAPIFlagsFile", "hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800238
239 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800240}
241
242// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
243// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800244func emptyFlagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800245 rule := android.NewRuleBuilder()
246
247 outputPath := hiddenAPISingletonPaths(ctx).flags
248
Colin Cross69f59a32019-02-15 10:39:37 -0800249 rule.Command().Text("rm").Flag("-f").Output(outputPath)
250 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800251
252 rule.Build(pctx, ctx, "emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800253
254 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800255}
256
257// metadataRule creates a rule to build hiddenapi-greylist.csv out of the metadata.csv files generated for boot image
258// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800259func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800260 var metadataCSV android.Paths
261
262 ctx.VisitAllModules(func(module android.Module) {
263 if h, ok := module.(hiddenAPIIntf); ok {
264 if csv := h.metadataCSV(); csv != nil {
265 metadataCSV = append(metadataCSV, csv)
266 }
267 }
268 })
269
270 rule := android.NewRuleBuilder()
271
272 outputPath := hiddenAPISingletonPaths(ctx).metadata
273
274 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800275 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/merge_csv.py")).
276 Inputs(metadataCSV).
Colin Crossf24a22a2019-01-31 14:12:44 -0800277 Text(">").
Colin Cross69f59a32019-02-15 10:39:37 -0800278 Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800279
280 rule.Build(pctx, ctx, "hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800281
282 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800283}
284
285// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
286// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
287// the rule.
288func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
289 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800290 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800291 rule.Command().
292 Text("(").
293 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800294 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800295 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800296 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800297 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800298 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800299 Text("fi").
300 Text(")")
301}