blob: 3030a927a0e4b2635c8e9688c5fc1350c0300813 [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 (
Paul Duffin1b033f52019-06-10 14:15:04 +010018 "fmt"
19
Colin Crossf24a22a2019-01-31 14:12:44 -080020 "android/soong/android"
21)
22
23func init() {
24 android.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
Paul Duffin1b033f52019-06-10 14:15:04 +010025 android.RegisterModuleType("hiddenapi_flags", hiddenAPIFlagsFactory)
Colin Crossf24a22a2019-01-31 14:12:44 -080026}
27
28type hiddenAPISingletonPathsStruct struct {
29 stubFlags android.OutputPath
30 flags android.OutputPath
31 metadata android.OutputPath
32}
33
34var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
35
36// hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
37// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
38// yet been created.
39func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
40 return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
41 return hiddenAPISingletonPathsStruct{
42 stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"),
43 flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"),
44 metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-greylist.csv"),
45 }
46 }).(hiddenAPISingletonPathsStruct)
47}
48
Colin Crossf24a22a2019-01-31 14:12:44 -080049func hiddenAPISingletonFactory() android.Singleton {
Colin Crossed023ec2019-02-19 12:38:45 -080050 return &hiddenAPISingleton{}
Colin Crossf24a22a2019-01-31 14:12:44 -080051}
52
Colin Crossed023ec2019-02-19 12:38:45 -080053type hiddenAPISingleton struct {
54 flags, metadata android.Path
55}
Colin Crossf24a22a2019-01-31 14:12:44 -080056
57// hiddenAPI singleton rules
Colin Crossed023ec2019-02-19 12:38:45 -080058func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Crossf24a22a2019-01-31 14:12:44 -080059 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
60 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
61 return
62 }
63
64 stubFlagsRule(ctx)
65
66 // These rules depend on files located in frameworks/base, skip them if running in a tree that doesn't have them.
Jiyong Park09cb6292019-07-15 15:29:23 +090067 if ctx.Config().FrameworksBaseDirExists(ctx) {
Colin Crossed023ec2019-02-19 12:38:45 -080068 h.flags = flagsRule(ctx)
69 h.metadata = metadataRule(ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -080070 } else {
Colin Crossed023ec2019-02-19 12:38:45 -080071 h.flags = emptyFlagsRule(ctx)
72 }
73}
74
75// Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
76// Both paths are used to call dist-for-goals.
77func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) {
78 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
79 return
80 }
81
82 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String())
83
84 if h.metadata != nil {
85 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String())
Colin Crossf24a22a2019-01-31 14:12:44 -080086 }
87}
88
89// stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image
90// modules.
91func stubFlagsRule(ctx android.SingletonContext) {
92 // Public API stubs
93 publicStubModules := []string{
94 "android_stubs_current",
Paul Duffin719fed42019-02-28 16:15:44 +000095 }
96
97 // Add the android.test.base to the set of stubs only if the android.test.base module is on
98 // the boot jars list as the runtime will only enforce hiddenapi access against modules on
99 // that list.
100 if inList("android.test.base", ctx.Config().BootJars()) {
101 publicStubModules = append(publicStubModules, "android.test.base.stubs")
Colin Crossf24a22a2019-01-31 14:12:44 -0800102 }
103
104 // System API stubs
105 systemStubModules := []string{
106 "android_system_stubs_current",
107 }
108
109 // Test API stubs
110 testStubModules := []string{
111 "android_test_stubs_current",
112 }
113
114 // Core Platform API stubs
115 corePlatformStubModules := []string{
116 "core.platform.api.stubs",
117 }
118
119 // Allow products to define their own stubs for custom product jars that apps can use.
120 publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
121 systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
122 testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
Allen Hairde816cf2019-02-25 16:37:42 -0800123 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") {
124 publicStubModules = append(publicStubModules, "jacoco-stubs")
125 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800126
127 publicStubPaths := make(android.Paths, len(publicStubModules))
128 systemStubPaths := make(android.Paths, len(systemStubModules))
129 testStubPaths := make(android.Paths, len(testStubModules))
130 corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules))
131
132 moduleListToPathList := map[*[]string]android.Paths{
133 &publicStubModules: publicStubPaths,
134 &systemStubModules: systemStubPaths,
135 &testStubModules: testStubPaths,
136 &corePlatformStubModules: corePlatformStubPaths,
137 }
138
139 var bootDexJars android.Paths
140
141 ctx.VisitAllModules(func(module android.Module) {
142 // Collect dex jar paths for the modules listed above.
143 if j, ok := module.(Dependency); ok {
144 name := ctx.ModuleName(module)
145 for moduleList, pathList := range moduleListToPathList {
146 if i := android.IndexList(name, *moduleList); i != -1 {
147 pathList[i] = j.DexJar()
148 }
149 }
150 }
151
152 // Collect dex jar paths for modules that had hiddenapi encode called on them.
153 if h, ok := module.(hiddenAPIIntf); ok {
154 if jar := h.bootDexJar(); jar != nil {
155 bootDexJars = append(bootDexJars, jar)
156 }
157 }
158 })
159
160 var missingDeps []string
161 // Ensure all modules were converted to paths
162 for moduleList, pathList := range moduleListToPathList {
163 for i := range pathList {
164 if pathList[i] == nil {
Colin Crosscaa0e1e2019-04-02 13:03:46 -0700165 pathList[i] = android.PathForOutput(ctx, "missing")
Colin Crossf24a22a2019-01-31 14:12:44 -0800166 if ctx.Config().AllowMissingDependencies() {
167 missingDeps = append(missingDeps, (*moduleList)[i])
Colin Crossf24a22a2019-01-31 14:12:44 -0800168 } else {
169 ctx.Errorf("failed to find dex jar path for module %q",
170 (*moduleList)[i])
171 }
172 }
173 }
174 }
175
176 // Singleton rule which applies hiddenapi on all boot class path dex files.
177 rule := android.NewRuleBuilder()
178
179 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
180 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
181
182 rule.MissingDeps(missingDeps)
183
184 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800185 Tool(pctx.HostBinToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800186 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800187 FlagForEachInput("--boot-dex=", bootDexJars).
188 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
Andrei Oneae04da072019-03-01 17:44:13 +0000189 FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":").
190 FlagWithInputList("--test-stub-classpath=", testStubPaths, ":").
Colin Cross69f59a32019-02-15 10:39:37 -0800191 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
192 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800193
194 commitChangeForRestat(rule, tempPath, outputPath)
195
196 rule.Build(pctx, ctx, "hiddenAPIStubFlagsFile", "hiddenapi stub flags")
197}
198
199// flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and
200// the greylists.
Colin Crossed023ec2019-02-19 12:38:45 -0800201func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800202 var flagsCSV android.Paths
203
204 var greylistIgnoreConflicts android.Path
205
206 ctx.VisitAllModules(func(module android.Module) {
207 if h, ok := module.(hiddenAPIIntf); ok {
208 if csv := h.flagsCSV(); csv != nil {
209 flagsCSV = append(flagsCSV, csv)
210 }
211 } else if ds, ok := module.(*Droidstubs); ok && ctx.ModuleName(module) == "hiddenapi-lists-docs" {
212 greylistIgnoreConflicts = ds.removedDexApiFile
213 }
214 })
215
216 if greylistIgnoreConflicts == nil {
217 ctx.Errorf("failed to find removed_dex_api_filename from hiddenapi-lists-docs module")
Colin Crossed023ec2019-02-19 12:38:45 -0800218 return nil
Colin Crossf24a22a2019-01-31 14:12:44 -0800219 }
220
221 rule := android.NewRuleBuilder()
222
223 outputPath := hiddenAPISingletonPaths(ctx).flags
224 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
225
226 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
227
228 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800229 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")).
230 FlagWithInput("--csv ", stubFlags).
231 Inputs(flagsCSV).
Colin Crossf24a22a2019-01-31 14:12:44 -0800232 FlagWithInput("--greylist ",
Colin Cross69f59a32019-02-15 10:39:37 -0800233 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800234 FlagWithInput("--greylist-ignore-conflicts ",
Colin Cross69f59a32019-02-15 10:39:37 -0800235 greylistIgnoreConflicts).
Colin Crossf24a22a2019-01-31 14:12:44 -0800236 FlagWithInput("--greylist-max-p ",
Colin Cross69f59a32019-02-15 10:39:37 -0800237 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800238 FlagWithInput("--greylist-max-o-ignore-conflicts ",
Colin Cross69f59a32019-02-15 10:39:37 -0800239 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800240 FlagWithInput("--blacklist ",
Colin Cross69f59a32019-02-15 10:39:37 -0800241 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt")).
Andrei Onea896237b2019-03-29 13:45:58 +0000242 FlagWithInput("--greylist-packages ",
243 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-packages.txt")).
Colin Cross69f59a32019-02-15 10:39:37 -0800244 FlagWithOutput("--output ", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800245
246 commitChangeForRestat(rule, tempPath, outputPath)
247
248 rule.Build(pctx, ctx, "hiddenAPIFlagsFile", "hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800249
250 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800251}
252
253// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
254// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800255func emptyFlagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800256 rule := android.NewRuleBuilder()
257
258 outputPath := hiddenAPISingletonPaths(ctx).flags
259
Colin Cross69f59a32019-02-15 10:39:37 -0800260 rule.Command().Text("rm").Flag("-f").Output(outputPath)
261 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800262
263 rule.Build(pctx, ctx, "emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800264
265 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800266}
267
268// metadataRule creates a rule to build hiddenapi-greylist.csv out of the metadata.csv files generated for boot image
269// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800270func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800271 var metadataCSV android.Paths
272
273 ctx.VisitAllModules(func(module android.Module) {
274 if h, ok := module.(hiddenAPIIntf); ok {
275 if csv := h.metadataCSV(); csv != nil {
276 metadataCSV = append(metadataCSV, csv)
277 }
278 }
279 })
280
281 rule := android.NewRuleBuilder()
282
283 outputPath := hiddenAPISingletonPaths(ctx).metadata
284
285 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800286 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/merge_csv.py")).
287 Inputs(metadataCSV).
Colin Crossf24a22a2019-01-31 14:12:44 -0800288 Text(">").
Colin Cross69f59a32019-02-15 10:39:37 -0800289 Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800290
291 rule.Build(pctx, ctx, "hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800292
293 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800294}
295
296// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
297// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
298// the rule.
299func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
300 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800301 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800302 rule.Command().
303 Text("(").
304 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800305 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800306 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800307 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800308 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800309 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800310 Text("fi").
311 Text(")")
312}
Paul Duffin1b033f52019-06-10 14:15:04 +0100313
314type hiddenAPIFlagsProperties struct {
315 // name of the file into which the flags will be copied.
316 Filename *string
317}
318
319type hiddenAPIFlags struct {
320 android.ModuleBase
321
322 properties hiddenAPIFlagsProperties
323
324 outputFilePath android.OutputPath
325}
326
327func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) {
328 filename := String(h.properties.Filename)
329
330 inputPath := hiddenAPISingletonPaths(ctx).flags
331 h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
332
333 // This ensures that outputFilePath has the correct name for others to
334 // use, as the source file may have a different name.
335 ctx.Build(pctx, android.BuildParams{
336 Rule: android.Cp,
337 Output: h.outputFilePath,
338 Input: inputPath,
339 })
340}
341
342func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) {
343 switch tag {
344 case "":
345 return android.Paths{h.outputFilePath}, nil
346 default:
347 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
348 }
349}
350
351// hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build.
352func hiddenAPIFlagsFactory() android.Module {
353 module := &hiddenAPIFlags{}
354 module.AddProperties(&module.properties)
355 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
356 return module
357}