blob: 7afba2a7906b7538733ed2b04eb4473c699537d7 [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)
Artur Satayevb5df8a02020-02-19 16:39:59 +000025 android.RegisterSingletonType("hiddenapi_index", hiddenAPIIndexSingletonFactory)
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 {
Colin Crossf24a22a2019-01-31 14:12:44 -080030 flags android.OutputPath
Artur Satayevb5df8a02020-02-19 16:39:59 +000031 index android.OutputPath
Colin Crossf24a22a2019-01-31 14:12:44 -080032 metadata android.OutputPath
Artur Satayevb5df8a02020-02-19 16:39:59 +000033 stubFlags android.OutputPath
Colin Crossf24a22a2019-01-31 14:12:44 -080034}
35
36var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
37
38// hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
39// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
40// yet been created.
41func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
42 return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
43 return hiddenAPISingletonPathsStruct{
Colin Crossf24a22a2019-01-31 14:12:44 -080044 flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"),
Artur Satayevb5df8a02020-02-19 16:39:59 +000045 index: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-index.csv"),
Colin Crossf24a22a2019-01-31 14:12:44 -080046 metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-greylist.csv"),
Artur Satayevb5df8a02020-02-19 16:39:59 +000047 stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"),
Colin Crossf24a22a2019-01-31 14:12:44 -080048 }
49 }).(hiddenAPISingletonPathsStruct)
50}
51
Colin Crossf24a22a2019-01-31 14:12:44 -080052func hiddenAPISingletonFactory() android.Singleton {
Colin Crossed023ec2019-02-19 12:38:45 -080053 return &hiddenAPISingleton{}
Colin Crossf24a22a2019-01-31 14:12:44 -080054}
55
Colin Crossed023ec2019-02-19 12:38:45 -080056type hiddenAPISingleton struct {
57 flags, metadata android.Path
58}
Colin Crossf24a22a2019-01-31 14:12:44 -080059
60// hiddenAPI singleton rules
Colin Crossed023ec2019-02-19 12:38:45 -080061func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Crossf24a22a2019-01-31 14:12:44 -080062 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
63 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
64 return
65 }
66
67 stubFlagsRule(ctx)
68
69 // 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 +090070 if ctx.Config().FrameworksBaseDirExists(ctx) {
Colin Crossed023ec2019-02-19 12:38:45 -080071 h.flags = flagsRule(ctx)
72 h.metadata = metadataRule(ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -080073 } else {
Colin Crossed023ec2019-02-19 12:38:45 -080074 h.flags = emptyFlagsRule(ctx)
75 }
76}
77
78// Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
79// Both paths are used to call dist-for-goals.
80func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) {
81 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
82 return
83 }
84
85 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String())
86
87 if h.metadata != nil {
88 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String())
Colin Crossf24a22a2019-01-31 14:12:44 -080089 }
90}
91
92// stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image
93// modules.
94func stubFlagsRule(ctx android.SingletonContext) {
95 // Public API stubs
96 publicStubModules := []string{
97 "android_stubs_current",
Paul Duffin719fed42019-02-28 16:15:44 +000098 }
99
100 // Add the android.test.base to the set of stubs only if the android.test.base module is on
101 // the boot jars list as the runtime will only enforce hiddenapi access against modules on
102 // that list.
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900103 if inList("android.test.base", ctx.Config().BootJars()) && !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Paul Duffin719fed42019-02-28 16:15:44 +0000104 publicStubModules = append(publicStubModules, "android.test.base.stubs")
Colin Crossf24a22a2019-01-31 14:12:44 -0800105 }
106
107 // System API stubs
108 systemStubModules := []string{
109 "android_system_stubs_current",
110 }
111
112 // Test API stubs
113 testStubModules := []string{
114 "android_test_stubs_current",
115 }
116
117 // Core Platform API stubs
118 corePlatformStubModules := []string{
Pete Gillin1f41dbf2020-06-02 15:59:45 +0100119 "legacy.core.platform.api.stubs",
Colin Crossf24a22a2019-01-31 14:12:44 -0800120 }
121
122 // Allow products to define their own stubs for custom product jars that apps can use.
123 publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
124 systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
125 testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
Allen Hairde816cf2019-02-25 16:37:42 -0800126 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") {
127 publicStubModules = append(publicStubModules, "jacoco-stubs")
128 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800129
130 publicStubPaths := make(android.Paths, len(publicStubModules))
131 systemStubPaths := make(android.Paths, len(systemStubModules))
132 testStubPaths := make(android.Paths, len(testStubModules))
133 corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules))
134
135 moduleListToPathList := map[*[]string]android.Paths{
136 &publicStubModules: publicStubPaths,
137 &systemStubModules: systemStubPaths,
138 &testStubModules: testStubPaths,
139 &corePlatformStubModules: corePlatformStubPaths,
140 }
141
142 var bootDexJars android.Paths
143
144 ctx.VisitAllModules(func(module android.Module) {
145 // Collect dex jar paths for the modules listed above.
146 if j, ok := module.(Dependency); ok {
147 name := ctx.ModuleName(module)
148 for moduleList, pathList := range moduleListToPathList {
149 if i := android.IndexList(name, *moduleList); i != -1 {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000150 pathList[i] = j.DexJarBuildPath()
Colin Crossf24a22a2019-01-31 14:12:44 -0800151 }
152 }
153 }
154
155 // Collect dex jar paths for modules that had hiddenapi encode called on them.
156 if h, ok := module.(hiddenAPIIntf); ok {
157 if jar := h.bootDexJar(); jar != nil {
Jiyong Park4ed468c2019-12-19 02:11:10 +0000158 // For a java lib included in an APEX, only take the one built for
159 // the platform variant, and skip the variants for APEXes.
160 // Otherwise, the hiddenapi tool will complain about duplicated classes
161 if a, ok := module.(android.ApexModule); ok {
162 if android.InAnyApex(module.Name()) && !a.IsForPlatform() {
Jiyong Park7f7766d2019-07-25 22:02:35 +0900163 return
164 }
165 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800166 bootDexJars = append(bootDexJars, jar)
167 }
168 }
169 })
170
171 var missingDeps []string
172 // Ensure all modules were converted to paths
173 for moduleList, pathList := range moduleListToPathList {
174 for i := range pathList {
175 if pathList[i] == nil {
Colin Crosscaa0e1e2019-04-02 13:03:46 -0700176 pathList[i] = android.PathForOutput(ctx, "missing")
Colin Crossf24a22a2019-01-31 14:12:44 -0800177 if ctx.Config().AllowMissingDependencies() {
178 missingDeps = append(missingDeps, (*moduleList)[i])
Colin Crossf24a22a2019-01-31 14:12:44 -0800179 } else {
180 ctx.Errorf("failed to find dex jar path for module %q",
181 (*moduleList)[i])
182 }
183 }
184 }
185 }
186
187 // Singleton rule which applies hiddenapi on all boot class path dex files.
188 rule := android.NewRuleBuilder()
189
190 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
191 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
192
193 rule.MissingDeps(missingDeps)
194
195 rule.Command().
Martin Stjernholm7260d062019-12-09 21:47:14 +0000196 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800197 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800198 FlagForEachInput("--boot-dex=", bootDexJars).
199 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
Andrei Oneae04da072019-03-01 17:44:13 +0000200 FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":").
201 FlagWithInputList("--test-stub-classpath=", testStubPaths, ":").
Colin Cross69f59a32019-02-15 10:39:37 -0800202 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
203 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800204
205 commitChangeForRestat(rule, tempPath, outputPath)
206
207 rule.Build(pctx, ctx, "hiddenAPIStubFlagsFile", "hiddenapi stub flags")
208}
209
210// flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000211// the unsupported API.
Colin Crossed023ec2019-02-19 12:38:45 -0800212func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800213 var flagsCSV android.Paths
Artur Satayevc7fb5c92020-03-25 16:48:49 +0000214 var greylistRemovedApis android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800215
216 ctx.VisitAllModules(func(module android.Module) {
217 if h, ok := module.(hiddenAPIIntf); ok {
218 if csv := h.flagsCSV(); csv != nil {
219 flagsCSV = append(flagsCSV, csv)
220 }
Artur Satayevc7fb5c92020-03-25 16:48:49 +0000221 } else if ds, ok := module.(*Droidstubs); ok {
222 // Track @removed public and system APIs via corresponding droidstubs targets.
223 // These APIs are not present in the stubs, however, we have to keep allowing access
224 // to them at runtime.
225 if m := ctx.ModuleName(module); m == "api-stubs-docs" || m == "system-api-stubs-docs" {
226 greylistRemovedApis = append(greylistRemovedApis, ds.removedDexApiFile)
227 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800228 }
229 })
230
Artur Satayevc7fb5c92020-03-25 16:48:49 +0000231 combinedRemovedApis := android.PathForOutput(ctx, "hiddenapi", "combined-removed-dex.txt")
232 ctx.Build(pctx, android.BuildParams{
233 Rule: android.Cat,
234 Inputs: greylistRemovedApis,
235 Output: combinedRemovedApis,
236 Description: "Combine removed apis for " + combinedRemovedApis.String(),
237 })
Colin Crossf24a22a2019-01-31 14:12:44 -0800238
239 rule := android.NewRuleBuilder()
240
241 outputPath := hiddenAPISingletonPaths(ctx).flags
242 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
243
244 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
245
246 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800247 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")).
248 FlagWithInput("--csv ", stubFlags).
249 Inputs(flagsCSV).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000250 FlagWithInput("--unsupported ",
Colin Cross69f59a32019-02-15 10:39:37 -0800251 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt")).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000252 FlagWithInput("--unsupported-ignore-conflicts ", combinedRemovedApis).
253 FlagWithInput("--max-target-q ",
Artur Satayevc5b4f992019-11-20 10:40:36 +0000254 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-q.txt")).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000255 FlagWithInput("--max-target-p ",
Colin Cross69f59a32019-02-15 10:39:37 -0800256 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt")).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000257 FlagWithInput("--max-target-o-ignore-conflicts ",
Colin Cross69f59a32019-02-15 10:39:37 -0800258 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt")).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000259 FlagWithInput("--blocked ",
Colin Cross69f59a32019-02-15 10:39:37 -0800260 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt")).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000261 FlagWithInput("--unsupported-packages ",
Andrei Onea896237b2019-03-29 13:45:58 +0000262 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-packages.txt")).
Colin Cross69f59a32019-02-15 10:39:37 -0800263 FlagWithOutput("--output ", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800264
265 commitChangeForRestat(rule, tempPath, outputPath)
266
267 rule.Build(pctx, ctx, "hiddenAPIFlagsFile", "hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800268
269 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800270}
271
272// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
273// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800274func emptyFlagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800275 rule := android.NewRuleBuilder()
276
277 outputPath := hiddenAPISingletonPaths(ctx).flags
278
Colin Cross69f59a32019-02-15 10:39:37 -0800279 rule.Command().Text("rm").Flag("-f").Output(outputPath)
280 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800281
282 rule.Build(pctx, ctx, "emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800283
284 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800285}
286
287// metadataRule creates a rule to build hiddenapi-greylist.csv out of the metadata.csv files generated for boot image
288// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800289func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800290 var metadataCSV android.Paths
291
292 ctx.VisitAllModules(func(module android.Module) {
293 if h, ok := module.(hiddenAPIIntf); ok {
294 if csv := h.metadataCSV(); csv != nil {
295 metadataCSV = append(metadataCSV, csv)
296 }
297 }
298 })
299
300 rule := android.NewRuleBuilder()
301
302 outputPath := hiddenAPISingletonPaths(ctx).metadata
303
304 rule.Command().
Artur Satayevb01dd442020-01-20 17:53:31 +0000305 BuiltTool(ctx, "merge_csv").
Artur Satayev79fac052020-01-20 19:11:33 +0000306 FlagWithOutput("--output=", outputPath).
307 Inputs(metadataCSV)
Colin Crossf24a22a2019-01-31 14:12:44 -0800308
309 rule.Build(pctx, ctx, "hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800310
311 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800312}
313
314// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
315// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
316// the rule.
317func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
318 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800319 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800320 rule.Command().
321 Text("(").
322 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800323 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800324 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800325 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800326 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800327 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800328 Text("fi").
329 Text(")")
330}
Paul Duffin1b033f52019-06-10 14:15:04 +0100331
332type hiddenAPIFlagsProperties struct {
333 // name of the file into which the flags will be copied.
334 Filename *string
335}
336
337type hiddenAPIFlags struct {
338 android.ModuleBase
339
340 properties hiddenAPIFlagsProperties
341
342 outputFilePath android.OutputPath
343}
344
345func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) {
346 filename := String(h.properties.Filename)
347
348 inputPath := hiddenAPISingletonPaths(ctx).flags
349 h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
350
351 // This ensures that outputFilePath has the correct name for others to
352 // use, as the source file may have a different name.
353 ctx.Build(pctx, android.BuildParams{
354 Rule: android.Cp,
355 Output: h.outputFilePath,
356 Input: inputPath,
357 })
358}
359
360func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) {
361 switch tag {
362 case "":
363 return android.Paths{h.outputFilePath}, nil
364 default:
365 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
366 }
367}
368
369// hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build.
370func hiddenAPIFlagsFactory() android.Module {
371 module := &hiddenAPIFlags{}
372 module.AddProperties(&module.properties)
373 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
374 return module
375}
Artur Satayevb5df8a02020-02-19 16:39:59 +0000376
377func hiddenAPIIndexSingletonFactory() android.Singleton {
378 return &hiddenAPIIndexSingleton{}
379}
380
381type hiddenAPIIndexSingleton struct {
382 index android.Path
383}
384
385func (h *hiddenAPIIndexSingleton) GenerateBuildActions(ctx android.SingletonContext) {
386 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
387 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
388 return
389 }
390
391 indexes := android.Paths{}
392 ctx.VisitAllModules(func(module android.Module) {
393 if h, ok := module.(hiddenAPIIntf); ok {
394 if h.indexCSV() != nil {
395 indexes = append(indexes, h.indexCSV())
396 }
397 }
398 })
399
400 rule := android.NewRuleBuilder()
401 rule.Command().
402 BuiltTool(ctx, "merge_csv").
403 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
404 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
405 Inputs(indexes)
406 rule.Build(pctx, ctx, "singleton-merged-hiddenapi-index", "Singleton merged Hidden API index")
407
408 h.index = hiddenAPISingletonPaths(ctx).index
409}
410
411func (h *hiddenAPIIndexSingleton) MakeVars(ctx android.MakeVarsContext) {
412 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
413 return
414 }
415
416 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_INDEX", h.index.String())
417}