blob: 8f34714d3bab5354a93367b6e35f2ce5d31e3587 [file] [log] [blame]
Colin Cross8faf8fc2019-01-16 15:15:52 -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 Duffind2aceca2019-02-28 16:13:20 +000018 "strings"
19
Colin Cross8faf8fc2019-01-16 15:15:52 -080020 "github.com/google/blueprint"
21
22 "android/soong/android"
23)
24
25var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
David Brazdil0f670a22019-01-18 16:30:03 +000026 Command: "${config.Class2Greylist} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
Colin Cross8faf8fc2019-01-16 15:15:52 -080027 CommandDeps: []string{"${config.Class2Greylist}"},
David Brazdil0f670a22019-01-18 16:30:03 +000028}, "outFlag", "stubAPIFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -080029
Colin Crossf24a22a2019-01-31 14:12:44 -080030type hiddenAPI struct {
31 flagsCSVPath android.Path
32 metadataCSVPath android.Path
33 bootDexJarPath android.Path
34}
35
36func (h *hiddenAPI) flagsCSV() android.Path {
37 return h.flagsCSVPath
38}
39
40func (h *hiddenAPI) metadataCSV() android.Path {
41 return h.metadataCSVPath
42}
43
44func (h *hiddenAPI) bootDexJar() android.Path {
45 return h.bootDexJarPath
46}
47
48type hiddenAPIIntf interface {
49 flagsCSV() android.Path
50 metadataCSV() android.Path
51 bootDexJar() android.Path
52}
53
54var _ hiddenAPIIntf = (*hiddenAPI)(nil)
55
56func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, dexJar android.ModuleOutPath, implementationJar android.Path,
57 uncompressDex bool) android.ModuleOutPath {
58
59 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
Paul Duffind2aceca2019-02-28 16:13:20 +000060 name := ctx.ModuleName()
61
62 // Modules whose names are of the format <x>-hiddenapi provide hiddenapi information
63 // for the boot jar module <x>. Otherwise, the module provides information for itself.
64 // Either way extract the name of the boot jar module.
65 bootJarName := strings.TrimSuffix(name, "-hiddenapi")
66
67 // If this module is on the boot jars list (or providing information for a module
68 // on the list) then extract the hiddenapi information from it, and if necessary
69 // encode that information in the generated dex file.
70 //
71 // It is important that hiddenapi information is only gathered for/from modules on
72 // that are actually on the boot jars list because the runtime only enforces access
73 // to the hidden API for the bootclassloader. If information is gathered for modules
74 // not on the list then that will cause failures in the CtsHiddenApiBlacklist...
75 // tests.
76 if inList(bootJarName, ctx.Config().BootJars()) {
Colin Crossf24a22a2019-01-31 14:12:44 -080077 // Derive the greylist from classes jar.
78 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
79 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
80 hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, implementationJar)
81 h.flagsCSVPath = flagsCSV
82 h.metadataCSVPath = metadataCSV
Paul Duffind2aceca2019-02-28 16:13:20 +000083
84 // If this module is actually on the boot jars list and not providing
85 // hiddenapi information for a module on the boot jars list then encode
86 // the gathered information in the generated dex file.
87 if name == bootJarName {
88 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar")
89 h.bootDexJarPath = dexJar
90 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
91 dexJar = hiddenAPIJar
92 }
Colin Crossf24a22a2019-01-31 14:12:44 -080093 }
94 }
95
96 return dexJar
97}
98
99func hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV android.WritablePath,
100 classesJar android.Path) {
101
102 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800103
104 ctx.Build(pctx, android.BuildParams{
105 Rule: hiddenAPIGenerateCSVRule,
106 Description: "hiddenapi flags",
107 Input: classesJar,
108 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000109 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800110 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000111 "outFlag": "--write-flags-csv",
112 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800113 },
114 })
115
116 ctx.Build(pctx, android.BuildParams{
117 Rule: hiddenAPIGenerateCSVRule,
118 Description: "hiddenapi metadata",
119 Input: classesJar,
120 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000121 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800122 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000123 "outFlag": "--write-metadata-csv",
124 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800125 },
126 })
127
Colin Cross8faf8fc2019-01-16 15:15:52 -0800128}
129
130var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
131 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output && ` +
132 `unzip -o -q $in 'classes*.dex' -d $tmpDir/dex-input && ` +
133 `for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do ` +
134 ` echo "--input-dex=$${INPUT_DEX}"; ` +
135 ` echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})"; ` +
David Brazdil91b4e3e2019-01-23 21:04:05 +0000136 `done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && ` +
Colin Crosscd964b32019-01-18 22:03:02 -0800137 `${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && ` +
Colin Cross8faf8fc2019-01-16 15:15:52 -0800138 `${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" $out $tmpDir/dex.jar $in`,
139 CommandDeps: []string{
140 "${config.HiddenAPI}",
141 "${config.SoongZipCmd}",
142 "${config.MergeZipsCmd}",
143 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000144}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800145
Colin Crossf24a22a2019-01-31 14:12:44 -0800146func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800147 uncompressDex bool) {
148
Colin Crossf24a22a2019-01-31 14:12:44 -0800149 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800150
Colin Crosscd964b32019-01-18 22:03:02 -0800151 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
152 // in the input it stays uncompressed in the output.
153 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000154 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000155 tmpOutput := output
156 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800157 if uncompressDex {
158 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000159 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
160 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800161 }
Jiyong Park93e57a02020-02-21 16:04:53 +0900162
163 enforceHiddenApiFlagsToAllMembers := true
David Brazdil91b4e3e2019-01-23 21:04:05 +0000164 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
165 // Disable assertion that all methods/fields have hidden API flags assigned.
166 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900167 enforceHiddenApiFlagsToAllMembers = false
168 }
169 // b/149353192: when a module is instrumented, jacoco adds synthetic members
170 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
171 // don't complain when we don't find hidden API flags for the synthetic members.
172 if j, ok := ctx.Module().(*Library); ok && j.shouldInstrument(ctx) {
173 enforceHiddenApiFlagsToAllMembers = false
174 }
175
176 if !enforceHiddenApiFlagsToAllMembers {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000177 hiddenapiFlags = "--no-force-assign-all"
178 }
Colin Crosscd964b32019-01-18 22:03:02 -0800179
Colin Cross8faf8fc2019-01-16 15:15:52 -0800180 ctx.Build(pctx, android.BuildParams{
181 Rule: hiddenAPIEncodeDexRule,
182 Description: "hiddenapi encode dex",
183 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000184 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800185 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800186 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800187 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000188 "tmpDir": tmpDir.String(),
189 "soongZipFlags": soongZipFlags,
190 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800191 },
192 })
193
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000194 if uncompressDex {
195 TransformZipAlign(ctx, output, tmpOutput)
196 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800197}