blob: c7dac8feceb17badccee28b6d3d26c5b33632681 [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{
Andrei Onea23fea042020-08-12 16:48:23 +010026 Command: "${config.Class2NonSdkList} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
27 CommandDeps: []string{"${config.Class2NonSdkList}"},
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 {
Paul Duffinff774a02021-01-29 12:53:15 +000031 // The path to the dex jar that is in the boot class path. If this is nil then the associated
32 // module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional
33 // annotations for the <x> boot dex jar but which do not actually provide a boot dex jar
34 // themselves.
35 bootDexJarPath android.Path
36
37 // The path to the CSV file that contains mappings from Java signature to various flags derived
38 // from annotations in the source, e.g. whether it is public or the sdk version above which it
39 // can no longer be used.
40 //
41 // It is created by the Class2NonSdkList tool which processes the .class files in the class
42 // implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
43 // tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
44 // consistency checks on the information in the annotations and to filter out bridge methods
45 // that are already part of the public API.
46 flagsCSVPath android.Path
47
48 // The path to the CSV file that contains mappings from Java signature to the value of properties
49 // specified on UnsupportedAppUsage annotations in the source.
50 //
51 // Like the flagsCSVPath file this is also created by the Class2NonSdkList in the same way.
52 // Although the two files could potentially be created in a single invocation of the
53 // Class2NonSdkList at the moment they are created using their own invocation, with the behavior
54 // being determined by the property that is used.
Artur Satayevb5df8a02020-02-19 16:39:59 +000055 metadataCSVPath android.Path
Paul Duffinff774a02021-01-29 12:53:15 +000056
57 // The path to the CSV file that contains mappings from Java signature to source location
58 // information.
59 //
60 // It is created by the merge_csv tool which processes the class implementation jar, extracting
61 // all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
62 // created by the unsupported app usage annotation processor during compilation of the class
63 // implementation jar.
64 indexCSVPath android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080065}
66
67func (h *hiddenAPI) flagsCSV() android.Path {
68 return h.flagsCSVPath
69}
70
71func (h *hiddenAPI) metadataCSV() android.Path {
72 return h.metadataCSVPath
73}
74
75func (h *hiddenAPI) bootDexJar() android.Path {
76 return h.bootDexJarPath
77}
78
Artur Satayevb5df8a02020-02-19 16:39:59 +000079func (h *hiddenAPI) indexCSV() android.Path {
80 return h.indexCSVPath
81}
82
Colin Crossf24a22a2019-01-31 14:12:44 -080083type hiddenAPIIntf interface {
Colin Crossf24a22a2019-01-31 14:12:44 -080084 bootDexJar() android.Path
Artur Satayevb5df8a02020-02-19 16:39:59 +000085 flagsCSV() android.Path
86 indexCSV() android.Path
87 metadataCSV() android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080088}
89
90var _ hiddenAPIIntf = (*hiddenAPI)(nil)
91
Paul Duffina2058f82020-06-24 16:22:38 +010092func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bool, dexJar android.ModuleOutPath,
Artur Satayevb5df8a02020-02-19 16:39:59 +000093 implementationJar android.Path, uncompressDex bool) android.ModuleOutPath {
Colin Crossf24a22a2019-01-31 14:12:44 -080094 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
Paul Duffind2aceca2019-02-28 16:13:20 +000095
96 // Modules whose names are of the format <x>-hiddenapi provide hiddenapi information
97 // for the boot jar module <x>. Otherwise, the module provides information for itself.
98 // Either way extract the name of the boot jar module.
99 bootJarName := strings.TrimSuffix(name, "-hiddenapi")
100
101 // If this module is on the boot jars list (or providing information for a module
102 // on the list) then extract the hiddenapi information from it, and if necessary
103 // encode that information in the generated dex file.
104 //
105 // It is important that hiddenapi information is only gathered for/from modules on
106 // that are actually on the boot jars list because the runtime only enforces access
107 // to the hidden API for the bootclassloader. If information is gathered for modules
108 // not on the list then that will cause failures in the CtsHiddenApiBlacklist...
109 // tests.
110 if inList(bootJarName, ctx.Config().BootJars()) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800111 // Derive the greylist from classes jar.
112 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
113 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000114 indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
115 h.hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, indexCSV, implementationJar)
Paul Duffind2aceca2019-02-28 16:13:20 +0000116
117 // If this module is actually on the boot jars list and not providing
118 // hiddenapi information for a module on the boot jars list then encode
119 // the gathered information in the generated dex file.
120 if name == bootJarName {
121 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar")
Paul Duffina2058f82020-06-24 16:22:38 +0100122
123 // More than one library with the same classes can be encoded but only one can
124 // be added to the global set of flags, otherwise it will result in duplicate
125 // classes which is an error. Therefore, only add the dex jar of one of them
126 // to the global set of flags.
127 if primary {
128 h.bootDexJarPath = dexJar
129 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000130 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
131 dexJar = hiddenAPIJar
132 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800133 }
134 }
135
136 return dexJar
137}
138
Artur Satayevb5df8a02020-02-19 16:39:59 +0000139func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV, indexCSV android.WritablePath, classesJar android.Path) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800140 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800141
142 ctx.Build(pctx, android.BuildParams{
143 Rule: hiddenAPIGenerateCSVRule,
144 Description: "hiddenapi flags",
145 Input: classesJar,
146 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000147 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800148 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000149 "outFlag": "--write-flags-csv",
150 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800151 },
152 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000153 h.flagsCSVPath = flagsCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800154
155 ctx.Build(pctx, android.BuildParams{
156 Rule: hiddenAPIGenerateCSVRule,
157 Description: "hiddenapi metadata",
158 Input: classesJar,
159 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000160 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800161 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000162 "outFlag": "--write-metadata-csv",
163 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800164 },
165 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000166 h.metadataCSVPath = metadataCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800167
Colin Crossf1a035e2020-11-16 17:32:30 -0800168 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000169 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800170 BuiltTool("merge_csv").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000171 FlagWithInput("--zip_input=", classesJar).
172 FlagWithOutput("--output=", indexCSV)
Colin Crossf1a035e2020-11-16 17:32:30 -0800173 rule.Build("merged-hiddenapi-index", "Merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000174 h.indexCSVPath = indexCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800175}
176
177var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayevb5df8a02020-02-19 16:39:59 +0000178 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
Colin Crossd783bbb2020-07-11 22:30:45 -0700179 unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
Artur Satayevb5df8a02020-02-19 16:39:59 +0000180 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
181 echo "--input-dex=$${INPUT_DEX}";
182 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
183 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
184 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
185 ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800186 CommandDeps: []string{
187 "${config.HiddenAPI}",
188 "${config.SoongZipCmd}",
189 "${config.MergeZipsCmd}",
190 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000191}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800192
Colin Crossf24a22a2019-01-31 14:12:44 -0800193func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800194 uncompressDex bool) {
195
Colin Crossf24a22a2019-01-31 14:12:44 -0800196 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800197
Colin Crosscd964b32019-01-18 22:03:02 -0800198 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
199 // in the input it stays uncompressed in the output.
200 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000201 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000202 tmpOutput := output
203 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800204 if uncompressDex {
205 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000206 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
207 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800208 }
Jiyong Park93e57a02020-02-21 16:04:53 +0900209
210 enforceHiddenApiFlagsToAllMembers := true
David Brazdil91b4e3e2019-01-23 21:04:05 +0000211 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
212 // Disable assertion that all methods/fields have hidden API flags assigned.
213 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900214 enforceHiddenApiFlagsToAllMembers = false
215 }
216 // b/149353192: when a module is instrumented, jacoco adds synthetic members
217 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
218 // don't complain when we don't find hidden API flags for the synthetic members.
Paul Duffinc495d2b2020-05-19 21:07:52 +0100219 if j, ok := ctx.Module().(interface {
220 shouldInstrument(android.BaseModuleContext) bool
221 }); ok && j.shouldInstrument(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900222 enforceHiddenApiFlagsToAllMembers = false
223 }
224
225 if !enforceHiddenApiFlagsToAllMembers {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000226 hiddenapiFlags = "--no-force-assign-all"
227 }
Colin Crosscd964b32019-01-18 22:03:02 -0800228
Colin Cross8faf8fc2019-01-16 15:15:52 -0800229 ctx.Build(pctx, android.BuildParams{
230 Rule: hiddenAPIEncodeDexRule,
231 Description: "hiddenapi encode dex",
232 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000233 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800234 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800235 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800236 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000237 "tmpDir": tmpDir.String(),
238 "soongZipFlags": soongZipFlags,
239 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800240 },
241 })
242
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000243 if uncompressDex {
244 TransformZipAlign(ctx, output, tmpOutput)
245 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800246}