blob: e9e4a45b00cd2d8c3a0d77ff83de5864de893ffb [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"
Joseph Murphy47522192019-12-19 01:06:24 +000019 "strings"
Paul Duffin1b033f52019-06-10 14:15:04 +010020
Colin Crossf24a22a2019-01-31 14:12:44 -080021 "android/soong/android"
22)
23
24func init() {
25 android.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
Paul Duffin1b033f52019-06-10 14:15:04 +010026 android.RegisterModuleType("hiddenapi_flags", hiddenAPIFlagsFactory)
Colin Crossf24a22a2019-01-31 14:12:44 -080027}
28
29type hiddenAPISingletonPathsStruct struct {
30 stubFlags android.OutputPath
31 flags android.OutputPath
32 metadata android.OutputPath
33}
34
35var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
36
37// hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
38// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
39// yet been created.
40func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
41 return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
42 return hiddenAPISingletonPathsStruct{
43 stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"),
44 flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"),
45 metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-greylist.csv"),
46 }
47 }).(hiddenAPISingletonPathsStruct)
48}
49
Colin Crossf24a22a2019-01-31 14:12:44 -080050func hiddenAPISingletonFactory() android.Singleton {
Colin Crossed023ec2019-02-19 12:38:45 -080051 return &hiddenAPISingleton{}
Colin Crossf24a22a2019-01-31 14:12:44 -080052}
53
Colin Crossed023ec2019-02-19 12:38:45 -080054type hiddenAPISingleton struct {
55 flags, metadata android.Path
56}
Colin Crossf24a22a2019-01-31 14:12:44 -080057
58// hiddenAPI singleton rules
Colin Crossed023ec2019-02-19 12:38:45 -080059func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Crossf24a22a2019-01-31 14:12:44 -080060 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
61 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
62 return
63 }
64
65 stubFlagsRule(ctx)
66
67 // 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 +090068 if ctx.Config().FrameworksBaseDirExists(ctx) {
Colin Crossed023ec2019-02-19 12:38:45 -080069 h.flags = flagsRule(ctx)
70 h.metadata = metadataRule(ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -080071 } else {
Colin Crossed023ec2019-02-19 12:38:45 -080072 h.flags = emptyFlagsRule(ctx)
73 }
74}
75
76// Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
77// Both paths are used to call dist-for-goals.
78func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) {
79 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
80 return
81 }
82
83 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String())
84
85 if h.metadata != nil {
86 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String())
Colin Crossf24a22a2019-01-31 14:12:44 -080087 }
88}
89
90// stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image
91// modules.
92func stubFlagsRule(ctx android.SingletonContext) {
93 // Public API stubs
94 publicStubModules := []string{
95 "android_stubs_current",
Paul Duffin719fed42019-02-28 16:15:44 +000096 }
97
98 // Add the android.test.base to the set of stubs only if the android.test.base module is on
99 // the boot jars list as the runtime will only enforce hiddenapi access against modules on
100 // that list.
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900101 if inList("android.test.base", ctx.Config().BootJars()) && !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Paul Duffin719fed42019-02-28 16:15:44 +0000102 publicStubModules = append(publicStubModules, "android.test.base.stubs")
Colin Crossf24a22a2019-01-31 14:12:44 -0800103 }
104
105 // System API stubs
106 systemStubModules := []string{
107 "android_system_stubs_current",
108 }
109
110 // Test API stubs
111 testStubModules := []string{
112 "android_test_stubs_current",
113 }
114
115 // Core Platform API stubs
116 corePlatformStubModules := []string{
117 "core.platform.api.stubs",
118 }
119
120 // Allow products to define their own stubs for custom product jars that apps can use.
121 publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
122 systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
123 testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
Allen Hairde816cf2019-02-25 16:37:42 -0800124 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") {
125 publicStubModules = append(publicStubModules, "jacoco-stubs")
126 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800127
128 publicStubPaths := make(android.Paths, len(publicStubModules))
129 systemStubPaths := make(android.Paths, len(systemStubModules))
130 testStubPaths := make(android.Paths, len(testStubModules))
131 corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules))
132
133 moduleListToPathList := map[*[]string]android.Paths{
134 &publicStubModules: publicStubPaths,
135 &systemStubModules: systemStubPaths,
136 &testStubModules: testStubPaths,
137 &corePlatformStubModules: corePlatformStubPaths,
138 }
139
140 var bootDexJars android.Paths
141
142 ctx.VisitAllModules(func(module android.Module) {
143 // Collect dex jar paths for the modules listed above.
144 if j, ok := module.(Dependency); ok {
145 name := ctx.ModuleName(module)
146 for moduleList, pathList := range moduleListToPathList {
147 if i := android.IndexList(name, *moduleList); i != -1 {
148 pathList[i] = j.DexJar()
149 }
150 }
151 }
152
153 // Collect dex jar paths for modules that had hiddenapi encode called on them.
154 if h, ok := module.(hiddenAPIIntf); ok {
155 if jar := h.bootDexJar(); jar != nil {
Joseph Murphy47522192019-12-19 01:06:24 +0000156 // Don't add multiple variants of the same library to bootDexJars, otherwise
157 // hiddenapi tool will complain about duplicated classes. Such multiple variants
158 // of the same library can happen when the library is included in one or more APEXes.
159 // TODO(b/146308764): remove this heuristic
160 if a, ok := module.(android.ApexModule); ok && android.InAnyApex(module.Name()) {
161 if a.AvailableFor("//apex_available:platform") && !a.IsForPlatform() {
162 // skip the apex variants if the jar is available for the platform
163 return
164 }
165 apexName := a.ApexName()
166 if strings.Contains(apexName, "test") {
167 // skip the if the jar is in test APEX
168 return
169 }
170
171 if strings.Contains(apexName, "com.android.art") && apexName != "com.android.art.release" {
172 // skip the ART APEX variants other than com.android.art.release
Jiyong Park7f7766d2019-07-25 22:02:35 +0900173 return
174 }
175 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800176 bootDexJars = append(bootDexJars, jar)
177 }
178 }
179 })
180
181 var missingDeps []string
182 // Ensure all modules were converted to paths
183 for moduleList, pathList := range moduleListToPathList {
184 for i := range pathList {
185 if pathList[i] == nil {
Colin Crosscaa0e1e2019-04-02 13:03:46 -0700186 pathList[i] = android.PathForOutput(ctx, "missing")
Colin Crossf24a22a2019-01-31 14:12:44 -0800187 if ctx.Config().AllowMissingDependencies() {
188 missingDeps = append(missingDeps, (*moduleList)[i])
Colin Crossf24a22a2019-01-31 14:12:44 -0800189 } else {
190 ctx.Errorf("failed to find dex jar path for module %q",
191 (*moduleList)[i])
192 }
193 }
194 }
195 }
196
197 // Singleton rule which applies hiddenapi on all boot class path dex files.
198 rule := android.NewRuleBuilder()
199
200 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
201 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
202
203 rule.MissingDeps(missingDeps)
204
205 rule.Command().
Martin Stjernholm7260d062019-12-09 21:47:14 +0000206 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800207 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800208 FlagForEachInput("--boot-dex=", bootDexJars).
209 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
Andrei Oneae04da072019-03-01 17:44:13 +0000210 FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":").
211 FlagWithInputList("--test-stub-classpath=", testStubPaths, ":").
Colin Cross69f59a32019-02-15 10:39:37 -0800212 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
213 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800214
215 commitChangeForRestat(rule, tempPath, outputPath)
216
217 rule.Build(pctx, ctx, "hiddenAPIStubFlagsFile", "hiddenapi stub flags")
218}
219
220// flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and
221// the greylists.
Colin Crossed023ec2019-02-19 12:38:45 -0800222func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800223 var flagsCSV android.Paths
224
225 var greylistIgnoreConflicts android.Path
226
227 ctx.VisitAllModules(func(module android.Module) {
228 if h, ok := module.(hiddenAPIIntf); ok {
229 if csv := h.flagsCSV(); csv != nil {
230 flagsCSV = append(flagsCSV, csv)
231 }
232 } else if ds, ok := module.(*Droidstubs); ok && ctx.ModuleName(module) == "hiddenapi-lists-docs" {
233 greylistIgnoreConflicts = ds.removedDexApiFile
234 }
235 })
236
237 if greylistIgnoreConflicts == nil {
238 ctx.Errorf("failed to find removed_dex_api_filename from hiddenapi-lists-docs module")
Colin Crossed023ec2019-02-19 12:38:45 -0800239 return nil
Colin Crossf24a22a2019-01-31 14:12:44 -0800240 }
241
242 rule := android.NewRuleBuilder()
243
244 outputPath := hiddenAPISingletonPaths(ctx).flags
245 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
246
247 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
248
249 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800250 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")).
251 FlagWithInput("--csv ", stubFlags).
252 Inputs(flagsCSV).
Colin Crossf24a22a2019-01-31 14:12:44 -0800253 FlagWithInput("--greylist ",
Colin Cross69f59a32019-02-15 10:39:37 -0800254 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800255 FlagWithInput("--greylist-ignore-conflicts ",
Colin Cross69f59a32019-02-15 10:39:37 -0800256 greylistIgnoreConflicts).
Artur Satayevc5b4f992019-11-20 10:40:36 +0000257 FlagWithInput("--greylist-max-q ",
258 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-q.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800259 FlagWithInput("--greylist-max-p ",
Colin Cross69f59a32019-02-15 10:39:37 -0800260 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800261 FlagWithInput("--greylist-max-o-ignore-conflicts ",
Colin Cross69f59a32019-02-15 10:39:37 -0800262 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800263 FlagWithInput("--blacklist ",
Colin Cross69f59a32019-02-15 10:39:37 -0800264 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt")).
Andrei Onea896237b2019-03-29 13:45:58 +0000265 FlagWithInput("--greylist-packages ",
266 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-packages.txt")).
Colin Cross69f59a32019-02-15 10:39:37 -0800267 FlagWithOutput("--output ", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800268
269 commitChangeForRestat(rule, tempPath, outputPath)
270
271 rule.Build(pctx, ctx, "hiddenAPIFlagsFile", "hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800272
273 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800274}
275
276// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
277// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800278func emptyFlagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800279 rule := android.NewRuleBuilder()
280
281 outputPath := hiddenAPISingletonPaths(ctx).flags
282
Colin Cross69f59a32019-02-15 10:39:37 -0800283 rule.Command().Text("rm").Flag("-f").Output(outputPath)
284 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800285
286 rule.Build(pctx, ctx, "emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800287
288 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800289}
290
291// metadataRule creates a rule to build hiddenapi-greylist.csv out of the metadata.csv files generated for boot image
292// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800293func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800294 var metadataCSV android.Paths
295
296 ctx.VisitAllModules(func(module android.Module) {
297 if h, ok := module.(hiddenAPIIntf); ok {
298 if csv := h.metadataCSV(); csv != nil {
299 metadataCSV = append(metadataCSV, csv)
300 }
301 }
302 })
303
304 rule := android.NewRuleBuilder()
305
306 outputPath := hiddenAPISingletonPaths(ctx).metadata
307
308 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800309 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/merge_csv.py")).
310 Inputs(metadataCSV).
Colin Crossf24a22a2019-01-31 14:12:44 -0800311 Text(">").
Colin Cross69f59a32019-02-15 10:39:37 -0800312 Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800313
314 rule.Build(pctx, ctx, "hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800315
316 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800317}
318
319// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
320// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
321// the rule.
322func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
323 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800324 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800325 rule.Command().
326 Text("(").
327 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800328 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800329 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800330 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800331 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800332 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800333 Text("fi").
334 Text(")")
335}
Paul Duffin1b033f52019-06-10 14:15:04 +0100336
337type hiddenAPIFlagsProperties struct {
338 // name of the file into which the flags will be copied.
339 Filename *string
340}
341
342type hiddenAPIFlags struct {
343 android.ModuleBase
344
345 properties hiddenAPIFlagsProperties
346
347 outputFilePath android.OutputPath
348}
349
350func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) {
351 filename := String(h.properties.Filename)
352
353 inputPath := hiddenAPISingletonPaths(ctx).flags
354 h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
355
356 // This ensures that outputFilePath has the correct name for others to
357 // use, as the source file may have a different name.
358 ctx.Build(pctx, android.BuildParams{
359 Rule: android.Cp,
360 Output: h.outputFilePath,
361 Input: inputPath,
362 })
363}
364
365func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) {
366 switch tag {
367 case "":
368 return android.Paths{h.outputFilePath}, nil
369 default:
370 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
371 }
372}
373
374// hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build.
375func hiddenAPIFlagsFactory() android.Module {
376 module := &hiddenAPIFlags{}
377 module.AddProperties(&module.properties)
378 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
379 return module
380}