blob: ada11c341813353b491e50e2f623ecbce841597c [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 Duffin612e6102021-02-02 13:38:13 +000092func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath,
93 implementationJar android.Path, uncompressDex bool) android.OutputPath {
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()) {
Paul Duffin219b1772021-02-04 17:49:45 +0000111 // More than one library with the same classes may need to be encoded but only one should be
112 // used as a source of information for hidden API processing otherwise it will result in
113 // duplicate entries in the files.
114 if primary {
115 // Create ninja rules to generate various CSV files needed by hiddenapi and store the paths
116 // in the hiddenAPI structure.
117 h.hiddenAPIGenerateCSV(ctx, implementationJar)
118
119 // Save the unencoded dex jar so it can be used when generating the
120 // hiddenAPISingletonPathsStruct.stubFlags file.
121 h.bootDexJarPath = dexJar
122 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000123
124 // If this module is actually on the boot jars list and not providing
125 // hiddenapi information for a module on the boot jars list then encode
126 // the gathered information in the generated dex file.
127 if name == bootJarName {
Paul Duffin612e6102021-02-02 13:38:13 +0000128 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar").OutputPath
Paul Duffina2058f82020-06-24 16:22:38 +0100129
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
Paul Duffin34982f12021-01-27 15:11:42 +0000139func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800140 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800141
Paul Duffin34982f12021-01-27 15:11:42 +0000142 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800143 ctx.Build(pctx, android.BuildParams{
144 Rule: hiddenAPIGenerateCSVRule,
145 Description: "hiddenapi flags",
146 Input: classesJar,
147 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000148 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800149 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000150 "outFlag": "--write-flags-csv",
151 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800152 },
153 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000154 h.flagsCSVPath = flagsCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800155
Paul Duffin34982f12021-01-27 15:11:42 +0000156 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800157 ctx.Build(pctx, android.BuildParams{
158 Rule: hiddenAPIGenerateCSVRule,
159 Description: "hiddenapi metadata",
160 Input: classesJar,
161 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000162 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800163 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000164 "outFlag": "--write-metadata-csv",
165 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800166 },
167 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000168 h.metadataCSVPath = metadataCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800169
Paul Duffin34982f12021-01-27 15:11:42 +0000170 indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
Colin Crossf1a035e2020-11-16 17:32:30 -0800171 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000172 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800173 BuiltTool("merge_csv").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000174 FlagWithInput("--zip_input=", classesJar).
175 FlagWithOutput("--output=", indexCSV)
Colin Crossf1a035e2020-11-16 17:32:30 -0800176 rule.Build("merged-hiddenapi-index", "Merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000177 h.indexCSVPath = indexCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800178}
179
180var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayevb5df8a02020-02-19 16:39:59 +0000181 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
Colin Crossd783bbb2020-07-11 22:30:45 -0700182 unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
Artur Satayevb5df8a02020-02-19 16:39:59 +0000183 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
184 echo "--input-dex=$${INPUT_DEX}";
185 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
186 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
187 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
188 ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800189 CommandDeps: []string{
190 "${config.HiddenAPI}",
191 "${config.SoongZipCmd}",
192 "${config.MergeZipsCmd}",
193 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000194}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800195
Colin Crossf24a22a2019-01-31 14:12:44 -0800196func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800197 uncompressDex bool) {
198
Colin Crossf24a22a2019-01-31 14:12:44 -0800199 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800200
Colin Crosscd964b32019-01-18 22:03:02 -0800201 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
202 // in the input it stays uncompressed in the output.
203 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000204 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000205 tmpOutput := output
206 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800207 if uncompressDex {
208 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000209 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
210 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800211 }
Jiyong Park93e57a02020-02-21 16:04:53 +0900212
213 enforceHiddenApiFlagsToAllMembers := true
David Brazdil91b4e3e2019-01-23 21:04:05 +0000214 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
215 // Disable assertion that all methods/fields have hidden API flags assigned.
216 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900217 enforceHiddenApiFlagsToAllMembers = false
218 }
219 // b/149353192: when a module is instrumented, jacoco adds synthetic members
220 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
221 // don't complain when we don't find hidden API flags for the synthetic members.
Paul Duffinc495d2b2020-05-19 21:07:52 +0100222 if j, ok := ctx.Module().(interface {
223 shouldInstrument(android.BaseModuleContext) bool
224 }); ok && j.shouldInstrument(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900225 enforceHiddenApiFlagsToAllMembers = false
226 }
227
228 if !enforceHiddenApiFlagsToAllMembers {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000229 hiddenapiFlags = "--no-force-assign-all"
230 }
Colin Crosscd964b32019-01-18 22:03:02 -0800231
Colin Cross8faf8fc2019-01-16 15:15:52 -0800232 ctx.Build(pctx, android.BuildParams{
233 Rule: hiddenAPIEncodeDexRule,
234 Description: "hiddenapi encode dex",
235 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000236 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800237 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800238 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800239 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000240 "tmpDir": tmpDir.String(),
241 "soongZipFlags": soongZipFlags,
242 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800243 },
244 })
245
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000246 if uncompressDex {
247 TransformZipAlign(ctx, output, tmpOutput)
248 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800249}