blob: 36ac0619fe26e69dbbcda4ea6e1f5866ccfe3cae [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 {
Colin Crossf24a22a2019-01-31 14:12:44 -080031 bootDexJarPath android.Path
Artur Satayev8a950792020-02-19 16:39:59 +000032 flagsCSVPath android.Path
33 indexCSVPath android.Path
34 metadataCSVPath android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080035}
36
37func (h *hiddenAPI) flagsCSV() android.Path {
38 return h.flagsCSVPath
39}
40
41func (h *hiddenAPI) metadataCSV() android.Path {
42 return h.metadataCSVPath
43}
44
45func (h *hiddenAPI) bootDexJar() android.Path {
46 return h.bootDexJarPath
47}
48
Artur Satayev8a950792020-02-19 16:39:59 +000049func (h *hiddenAPI) indexCSV() android.Path {
50 return h.indexCSVPath
51}
52
Colin Crossf24a22a2019-01-31 14:12:44 -080053type hiddenAPIIntf interface {
Colin Crossf24a22a2019-01-31 14:12:44 -080054 bootDexJar() android.Path
Artur Satayev8a950792020-02-19 16:39:59 +000055 flagsCSV() android.Path
56 indexCSV() android.Path
57 metadataCSV() android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080058}
59
60var _ hiddenAPIIntf = (*hiddenAPI)(nil)
61
Paul Duffinc4422102020-06-24 16:22:38 +010062func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bool, dexJar android.ModuleOutPath,
Artur Satayev8a950792020-02-19 16:39:59 +000063 implementationJar android.Path, uncompressDex bool) android.ModuleOutPath {
Colin Crossf24a22a2019-01-31 14:12:44 -080064 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
Paul Duffind2aceca2019-02-28 16:13:20 +000065
66 // Modules whose names are of the format <x>-hiddenapi provide hiddenapi information
67 // for the boot jar module <x>. Otherwise, the module provides information for itself.
68 // Either way extract the name of the boot jar module.
69 bootJarName := strings.TrimSuffix(name, "-hiddenapi")
70
71 // If this module is on the boot jars list (or providing information for a module
72 // on the list) then extract the hiddenapi information from it, and if necessary
73 // encode that information in the generated dex file.
74 //
75 // It is important that hiddenapi information is only gathered for/from modules on
76 // that are actually on the boot jars list because the runtime only enforces access
77 // to the hidden API for the bootclassloader. If information is gathered for modules
78 // not on the list then that will cause failures in the CtsHiddenApiBlacklist...
79 // tests.
Sam Mortimer3458e6a2019-10-07 11:41:14 -070080 isBootJarProvider := false
81 ctx.VisitAllModuleVariants(func(module android.Module) {
82 if m, ok := module.(interface{ BootJarProvider() bool }); ok &&
83 m.BootJarProvider() {
84 isBootJarProvider = true
85 }
86 })
87 if isBootJarProvider && inList(bootJarName, ctx.Config().BootJars()) {
Colin Crossf24a22a2019-01-31 14:12:44 -080088 // Derive the greylist from classes jar.
89 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
90 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
Artur Satayev8a950792020-02-19 16:39:59 +000091 indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
92 h.hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, indexCSV, implementationJar)
Paul Duffind2aceca2019-02-28 16:13:20 +000093
94 // If this module is actually on the boot jars list and not providing
95 // hiddenapi information for a module on the boot jars list then encode
96 // the gathered information in the generated dex file.
97 if name == bootJarName {
98 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar")
Paul Duffinc4422102020-06-24 16:22:38 +010099
100 // More than one library with the same classes can be encoded but only one can
101 // be added to the global set of flags, otherwise it will result in duplicate
102 // classes which is an error. Therefore, only add the dex jar of one of them
103 // to the global set of flags.
104 if primary {
105 h.bootDexJarPath = dexJar
106 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000107 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
108 dexJar = hiddenAPIJar
109 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800110 }
111 }
112
113 return dexJar
114}
115
Artur Satayev8a950792020-02-19 16:39:59 +0000116func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV, indexCSV android.WritablePath, classesJar android.Path) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800117 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800118
119 ctx.Build(pctx, android.BuildParams{
120 Rule: hiddenAPIGenerateCSVRule,
121 Description: "hiddenapi flags",
122 Input: classesJar,
123 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000124 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800125 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000126 "outFlag": "--write-flags-csv",
127 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800128 },
129 })
Artur Satayev8a950792020-02-19 16:39:59 +0000130 h.flagsCSVPath = flagsCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800131
132 ctx.Build(pctx, android.BuildParams{
133 Rule: hiddenAPIGenerateCSVRule,
134 Description: "hiddenapi metadata",
135 Input: classesJar,
136 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000137 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800138 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000139 "outFlag": "--write-metadata-csv",
140 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800141 },
142 })
Artur Satayev8a950792020-02-19 16:39:59 +0000143 h.metadataCSVPath = metadataCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800144
Artur Satayev8a950792020-02-19 16:39:59 +0000145 rule := android.NewRuleBuilder()
146 rule.Command().
147 BuiltTool(ctx, "merge_csv").
148 FlagWithInput("--zip_input=", classesJar).
149 FlagWithOutput("--output=", indexCSV)
150 rule.Build(pctx, ctx, "merged-hiddenapi-index", "Merged Hidden API index")
151 h.indexCSVPath = indexCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800152}
153
154var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayev8a950792020-02-19 16:39:59 +0000155 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
Colin Cross774a7582020-07-11 22:30:45 -0700156 unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
Artur Satayev8a950792020-02-19 16:39:59 +0000157 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
158 echo "--input-dex=$${INPUT_DEX}";
159 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
160 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
161 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
162 ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800163 CommandDeps: []string{
164 "${config.HiddenAPI}",
165 "${config.SoongZipCmd}",
166 "${config.MergeZipsCmd}",
167 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000168}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800169
Colin Crossf24a22a2019-01-31 14:12:44 -0800170func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800171 uncompressDex bool) {
172
Colin Crossf24a22a2019-01-31 14:12:44 -0800173 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800174
Colin Crosscd964b32019-01-18 22:03:02 -0800175 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
176 // in the input it stays uncompressed in the output.
177 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000178 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000179 tmpOutput := output
180 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800181 if uncompressDex {
182 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000183 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
184 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800185 }
Jiyong Park28826602020-02-21 16:04:53 +0900186
187 enforceHiddenApiFlagsToAllMembers := true
David Brazdil91b4e3e2019-01-23 21:04:05 +0000188 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
189 // Disable assertion that all methods/fields have hidden API flags assigned.
190 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Jiyong Park28826602020-02-21 16:04:53 +0900191 enforceHiddenApiFlagsToAllMembers = false
192 }
193 // b/149353192: when a module is instrumented, jacoco adds synthetic members
194 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
195 // don't complain when we don't find hidden API flags for the synthetic members.
Paul Duffin31b84c82020-05-19 21:07:52 +0100196 if j, ok := ctx.Module().(interface {
197 shouldInstrument(android.BaseModuleContext) bool
198 }); ok && j.shouldInstrument(ctx) {
Jiyong Park28826602020-02-21 16:04:53 +0900199 enforceHiddenApiFlagsToAllMembers = false
200 }
201
202 if !enforceHiddenApiFlagsToAllMembers {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000203 hiddenapiFlags = "--no-force-assign-all"
204 }
Colin Crosscd964b32019-01-18 22:03:02 -0800205
Colin Cross8faf8fc2019-01-16 15:15:52 -0800206 ctx.Build(pctx, android.BuildParams{
207 Rule: hiddenAPIEncodeDexRule,
208 Description: "hiddenapi encode dex",
209 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000210 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800211 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800212 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800213 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000214 "tmpDir": tmpDir.String(),
215 "soongZipFlags": soongZipFlags,
216 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800217 },
218 })
219
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000220 if uncompressDex {
221 TransformZipAlign(ctx, output, tmpOutput)
222 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800223}