blob: 8379f5397737290d1a79406704c5e3f288df8986 [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.
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900100 if inList("android.test.base", ctx.Config().BootJars()) && !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Paul Duffin719fed42019-02-28 16:15:44 +0000101 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 {
Jiyong Park7f7766d2019-07-25 22:02:35 +0900155 // For a java lib included in an APEX, only take the one built for
156 // the platform variant, and skip the variants for APEXes.
157 // Otherwise, the hiddenapi tool will complain about duplicated classes
158 if a, ok := module.(android.ApexModule); ok {
159 if android.InAnyApex(module.Name()) && !a.IsForPlatform() {
160 return
161 }
162 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800163 bootDexJars = append(bootDexJars, jar)
164 }
165 }
166 })
167
168 var missingDeps []string
169 // Ensure all modules were converted to paths
170 for moduleList, pathList := range moduleListToPathList {
171 for i := range pathList {
172 if pathList[i] == nil {
Colin Crosscaa0e1e2019-04-02 13:03:46 -0700173 pathList[i] = android.PathForOutput(ctx, "missing")
Colin Crossf24a22a2019-01-31 14:12:44 -0800174 if ctx.Config().AllowMissingDependencies() {
175 missingDeps = append(missingDeps, (*moduleList)[i])
Colin Crossf24a22a2019-01-31 14:12:44 -0800176 } else {
177 ctx.Errorf("failed to find dex jar path for module %q",
178 (*moduleList)[i])
179 }
180 }
181 }
182 }
183
184 // Singleton rule which applies hiddenapi on all boot class path dex files.
185 rule := android.NewRuleBuilder()
186
187 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
188 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
189
190 rule.MissingDeps(missingDeps)
191
192 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800193 Tool(pctx.HostBinToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800194 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800195 FlagForEachInput("--boot-dex=", bootDexJars).
196 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
Andrei Oneae04da072019-03-01 17:44:13 +0000197 FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":").
198 FlagWithInputList("--test-stub-classpath=", testStubPaths, ":").
Colin Cross69f59a32019-02-15 10:39:37 -0800199 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
200 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800201
202 commitChangeForRestat(rule, tempPath, outputPath)
203
204 rule.Build(pctx, ctx, "hiddenAPIStubFlagsFile", "hiddenapi stub flags")
205}
206
207// flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and
208// the greylists.
Colin Crossed023ec2019-02-19 12:38:45 -0800209func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800210 var flagsCSV android.Paths
211
212 var greylistIgnoreConflicts android.Path
213
214 ctx.VisitAllModules(func(module android.Module) {
215 if h, ok := module.(hiddenAPIIntf); ok {
216 if csv := h.flagsCSV(); csv != nil {
217 flagsCSV = append(flagsCSV, csv)
218 }
219 } else if ds, ok := module.(*Droidstubs); ok && ctx.ModuleName(module) == "hiddenapi-lists-docs" {
220 greylistIgnoreConflicts = ds.removedDexApiFile
221 }
222 })
223
224 if greylistIgnoreConflicts == nil {
225 ctx.Errorf("failed to find removed_dex_api_filename from hiddenapi-lists-docs module")
Colin Crossed023ec2019-02-19 12:38:45 -0800226 return nil
Colin Crossf24a22a2019-01-31 14:12:44 -0800227 }
228
229 rule := android.NewRuleBuilder()
230
231 outputPath := hiddenAPISingletonPaths(ctx).flags
232 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
233
234 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
235
236 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800237 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")).
238 FlagWithInput("--csv ", stubFlags).
239 Inputs(flagsCSV).
Colin Crossf24a22a2019-01-31 14:12:44 -0800240 FlagWithInput("--greylist ",
Colin Cross69f59a32019-02-15 10:39:37 -0800241 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800242 FlagWithInput("--greylist-ignore-conflicts ",
Colin Cross69f59a32019-02-15 10:39:37 -0800243 greylistIgnoreConflicts).
Colin Crossf24a22a2019-01-31 14:12:44 -0800244 FlagWithInput("--greylist-max-p ",
Colin Cross69f59a32019-02-15 10:39:37 -0800245 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800246 FlagWithInput("--greylist-max-o-ignore-conflicts ",
Colin Cross69f59a32019-02-15 10:39:37 -0800247 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800248 FlagWithInput("--blacklist ",
Colin Cross69f59a32019-02-15 10:39:37 -0800249 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt")).
Andrei Onea896237b2019-03-29 13:45:58 +0000250 FlagWithInput("--greylist-packages ",
251 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-packages.txt")).
Colin Cross69f59a32019-02-15 10:39:37 -0800252 FlagWithOutput("--output ", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800253
254 commitChangeForRestat(rule, tempPath, outputPath)
255
256 rule.Build(pctx, ctx, "hiddenAPIFlagsFile", "hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800257
258 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800259}
260
261// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
262// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800263func emptyFlagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800264 rule := android.NewRuleBuilder()
265
266 outputPath := hiddenAPISingletonPaths(ctx).flags
267
Colin Cross69f59a32019-02-15 10:39:37 -0800268 rule.Command().Text("rm").Flag("-f").Output(outputPath)
269 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800270
271 rule.Build(pctx, ctx, "emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800272
273 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800274}
275
276// metadataRule creates a rule to build hiddenapi-greylist.csv out of the metadata.csv files generated for boot image
277// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800278func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800279 var metadataCSV android.Paths
280
281 ctx.VisitAllModules(func(module android.Module) {
282 if h, ok := module.(hiddenAPIIntf); ok {
283 if csv := h.metadataCSV(); csv != nil {
284 metadataCSV = append(metadataCSV, csv)
285 }
286 }
287 })
288
289 rule := android.NewRuleBuilder()
290
291 outputPath := hiddenAPISingletonPaths(ctx).metadata
292
293 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800294 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/merge_csv.py")).
295 Inputs(metadataCSV).
Colin Crossf24a22a2019-01-31 14:12:44 -0800296 Text(">").
Colin Cross69f59a32019-02-15 10:39:37 -0800297 Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800298
299 rule.Build(pctx, ctx, "hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800300
301 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800302}
303
304// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
305// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
306// the rule.
307func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
308 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800309 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800310 rule.Command().
311 Text("(").
312 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800313 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800314 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800315 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800316 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800317 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800318 Text("fi").
319 Text(")")
320}
Paul Duffin1b033f52019-06-10 14:15:04 +0100321
322type hiddenAPIFlagsProperties struct {
323 // name of the file into which the flags will be copied.
324 Filename *string
325}
326
327type hiddenAPIFlags struct {
328 android.ModuleBase
329
330 properties hiddenAPIFlagsProperties
331
332 outputFilePath android.OutputPath
333}
334
335func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) {
336 filename := String(h.properties.Filename)
337
338 inputPath := hiddenAPISingletonPaths(ctx).flags
339 h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
340
341 // This ensures that outputFilePath has the correct name for others to
342 // use, as the source file may have a different name.
343 ctx.Build(pctx, android.BuildParams{
344 Rule: android.Cp,
345 Output: h.outputFilePath,
346 Input: inputPath,
347 })
348}
349
350func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) {
351 switch tag {
352 case "":
353 return android.Paths{h.outputFilePath}, nil
354 default:
355 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
356 }
357}
358
359// hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build.
360func hiddenAPIFlagsFactory() android.Module {
361 module := &hiddenAPIFlags{}
362 module.AddProperties(&module.properties)
363 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
364 return module
365}