blob: 7e34c83817a5a528c82666e9a6e9bd4171782a0c [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 Duffin4103e922021-02-01 19:01:34 +000031 // True if the module containing this structure contributes to the hiddenapi information.
32 active bool
33
34 // True if the module only contains additional annotations and so does not require hiddenapi
35 // information to be encoded in its dex file and should not be used to generate the
36 // hiddenAPISingletonPathsStruct.stubFlags file.
37 annotationsOnly bool
38
Paul Duffinff774a02021-01-29 12:53:15 +000039 // The path to the dex jar that is in the boot class path. If this is nil then the associated
40 // module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional
41 // annotations for the <x> boot dex jar but which do not actually provide a boot dex jar
42 // themselves.
Paul Duffin4103e922021-02-01 19:01:34 +000043 //
44 // This must be the path to the unencoded dex jar as the encoded dex jar indirectly depends on
45 // this file so using the encoded dex jar here would result in a cycle in the ninja rules.
Paul Duffinff774a02021-01-29 12:53:15 +000046 bootDexJarPath android.Path
47
48 // The path to the CSV file that contains mappings from Java signature to various flags derived
49 // from annotations in the source, e.g. whether it is public or the sdk version above which it
50 // can no longer be used.
51 //
52 // It is created by the Class2NonSdkList tool which processes the .class files in the class
53 // implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
54 // tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
55 // consistency checks on the information in the annotations and to filter out bridge methods
56 // that are already part of the public API.
57 flagsCSVPath android.Path
58
59 // The path to the CSV file that contains mappings from Java signature to the value of properties
60 // specified on UnsupportedAppUsage annotations in the source.
61 //
62 // Like the flagsCSVPath file this is also created by the Class2NonSdkList in the same way.
63 // Although the two files could potentially be created in a single invocation of the
64 // Class2NonSdkList at the moment they are created using their own invocation, with the behavior
65 // being determined by the property that is used.
Artur Satayevb5df8a02020-02-19 16:39:59 +000066 metadataCSVPath android.Path
Paul Duffinff774a02021-01-29 12:53:15 +000067
68 // The path to the CSV file that contains mappings from Java signature to source location
69 // information.
70 //
71 // It is created by the merge_csv tool which processes the class implementation jar, extracting
72 // all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
73 // created by the unsupported app usage annotation processor during compilation of the class
74 // implementation jar.
75 indexCSVPath android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080076}
77
78func (h *hiddenAPI) flagsCSV() android.Path {
79 return h.flagsCSVPath
80}
81
82func (h *hiddenAPI) metadataCSV() android.Path {
83 return h.metadataCSVPath
84}
85
86func (h *hiddenAPI) bootDexJar() android.Path {
87 return h.bootDexJarPath
88}
89
Artur Satayevb5df8a02020-02-19 16:39:59 +000090func (h *hiddenAPI) indexCSV() android.Path {
91 return h.indexCSVPath
92}
93
Colin Crossf24a22a2019-01-31 14:12:44 -080094type hiddenAPIIntf interface {
Colin Crossf24a22a2019-01-31 14:12:44 -080095 bootDexJar() android.Path
Artur Satayevb5df8a02020-02-19 16:39:59 +000096 flagsCSV() android.Path
97 indexCSV() android.Path
98 metadataCSV() android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080099}
100
101var _ hiddenAPIIntf = (*hiddenAPI)(nil)
102
Paul Duffin4103e922021-02-01 19:01:34 +0000103// Initialize the hiddenapi structure
104func (h *hiddenAPI) initHiddenAPI(ctx android.BaseModuleContext, name string) {
105 // If hiddenapi processing is disabled treat this as inactive.
106 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
107 return
108 }
109
110 // Modules whose names are of the format <x>-hiddenapi provide hiddenapi information for the boot
111 // jar module <x>. Otherwise, the module provides information for itself. Either way extract the
112 // name of the boot jar module.
113 bootJarName := strings.TrimSuffix(name, "-hiddenapi")
114
115 // It is important that hiddenapi information is only gathered for/from modules that are actually
116 // on the boot jars list because the runtime only enforces access to the hidden API for the
117 // bootclassloader. If information is gathered for modules not on the list then that will cause
118 // failures in the CtsHiddenApiBlocklist... tests.
119 h.active = inList(bootJarName, ctx.Config().BootJars())
120
121 // If this module has a suffix of -hiddenapi then it only provides additional annotation
122 // information for a module on the boot jars list.
123 h.annotationsOnly = strings.HasSuffix(name, "-hiddenapi")
124}
125
126// hiddenAPI is called by any module that could contribute to the hiddenapi processing.
127//
128// It ignores any module that has not had initHiddenApi() called on it and which is not in the boot
129// jar list.
130//
131// Otherwise, it generates ninja rules to do the following:
132// 1. Generates CSV files needed for hiddenapi processing.
133// 2. Conditionally adds the supplied dex file to the list of files used to generate the
134// hiddenAPISingletonPathsStruct.stubsFlag file.
135// 3. Conditionally creates a copy of the supplied dex file into which it has encoded the hiddenapi
136// flags and returns this instead of the supplied dex jar, otherwise simply returns the supplied
137// dex jar.
Paul Duffin612e6102021-02-02 13:38:13 +0000138func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath,
139 implementationJar android.Path, uncompressDex bool) android.OutputPath {
Paul Duffind2aceca2019-02-28 16:13:20 +0000140
Paul Duffin4103e922021-02-01 19:01:34 +0000141 if !h.active {
142 return dexJar
143 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000144
Paul Duffin4103e922021-02-01 19:01:34 +0000145 // More than one library with the same classes may need to be encoded but only one should be
146 // used as a source of information for hidden API processing otherwise it will result in
147 // duplicate entries in the files.
148 if primary {
149 // Create ninja rules to generate various CSV files needed by hiddenapi and store the paths
150 // in the hiddenAPI structure.
151 h.hiddenAPIGenerateCSV(ctx, implementationJar)
Paul Duffin219b1772021-02-04 17:49:45 +0000152
Paul Duffin4103e922021-02-01 19:01:34 +0000153 // Save the unencoded dex jar so it can be used when generating the
154 // hiddenAPISingletonPathsStruct.stubFlags file.
155 h.bootDexJarPath = dexJar
156 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000157
Paul Duffin4103e922021-02-01 19:01:34 +0000158 if !h.annotationsOnly {
159 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar").OutputPath
Paul Duffina2058f82020-06-24 16:22:38 +0100160
Paul Duffin4103e922021-02-01 19:01:34 +0000161 // Create a copy of the dex jar which has been encoded with hiddenapi flags.
162 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
163
164 // Use the encoded dex jar from here onwards.
165 dexJar = hiddenAPIJar
Colin Crossf24a22a2019-01-31 14:12:44 -0800166 }
167
168 return dexJar
169}
170
Paul Duffin34982f12021-01-27 15:11:42 +0000171func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800172 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800173
Paul Duffin34982f12021-01-27 15:11:42 +0000174 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800175 ctx.Build(pctx, android.BuildParams{
176 Rule: hiddenAPIGenerateCSVRule,
177 Description: "hiddenapi flags",
178 Input: classesJar,
179 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000180 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800181 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000182 "outFlag": "--write-flags-csv",
183 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800184 },
185 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000186 h.flagsCSVPath = flagsCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800187
Paul Duffin34982f12021-01-27 15:11:42 +0000188 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800189 ctx.Build(pctx, android.BuildParams{
190 Rule: hiddenAPIGenerateCSVRule,
191 Description: "hiddenapi metadata",
192 Input: classesJar,
193 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000194 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800195 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000196 "outFlag": "--write-metadata-csv",
197 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800198 },
199 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000200 h.metadataCSVPath = metadataCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800201
Paul Duffin34982f12021-01-27 15:11:42 +0000202 indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
Colin Crossf1a035e2020-11-16 17:32:30 -0800203 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000204 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800205 BuiltTool("merge_csv").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000206 FlagWithInput("--zip_input=", classesJar).
207 FlagWithOutput("--output=", indexCSV)
Colin Crossf1a035e2020-11-16 17:32:30 -0800208 rule.Build("merged-hiddenapi-index", "Merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000209 h.indexCSVPath = indexCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800210}
211
212var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayevb5df8a02020-02-19 16:39:59 +0000213 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
Colin Crossd783bbb2020-07-11 22:30:45 -0700214 unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
Artur Satayevb5df8a02020-02-19 16:39:59 +0000215 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
216 echo "--input-dex=$${INPUT_DEX}";
217 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
218 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
219 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
220 ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800221 CommandDeps: []string{
222 "${config.HiddenAPI}",
223 "${config.SoongZipCmd}",
224 "${config.MergeZipsCmd}",
225 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000226}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800227
Colin Crossf24a22a2019-01-31 14:12:44 -0800228func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800229 uncompressDex bool) {
230
Colin Crossf24a22a2019-01-31 14:12:44 -0800231 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800232
Colin Crosscd964b32019-01-18 22:03:02 -0800233 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
234 // in the input it stays uncompressed in the output.
235 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000236 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000237 tmpOutput := output
238 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800239 if uncompressDex {
240 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000241 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
242 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800243 }
Jiyong Park93e57a02020-02-21 16:04:53 +0900244
245 enforceHiddenApiFlagsToAllMembers := true
David Brazdil91b4e3e2019-01-23 21:04:05 +0000246 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
247 // Disable assertion that all methods/fields have hidden API flags assigned.
248 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900249 enforceHiddenApiFlagsToAllMembers = false
250 }
251 // b/149353192: when a module is instrumented, jacoco adds synthetic members
252 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
253 // don't complain when we don't find hidden API flags for the synthetic members.
Paul Duffinc495d2b2020-05-19 21:07:52 +0100254 if j, ok := ctx.Module().(interface {
255 shouldInstrument(android.BaseModuleContext) bool
256 }); ok && j.shouldInstrument(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900257 enforceHiddenApiFlagsToAllMembers = false
258 }
259
260 if !enforceHiddenApiFlagsToAllMembers {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000261 hiddenapiFlags = "--no-force-assign-all"
262 }
Colin Crosscd964b32019-01-18 22:03:02 -0800263
Colin Cross8faf8fc2019-01-16 15:15:52 -0800264 ctx.Build(pctx, android.BuildParams{
265 Rule: hiddenAPIEncodeDexRule,
266 Description: "hiddenapi encode dex",
267 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000268 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800269 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800270 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800271 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000272 "tmpDir": tmpDir.String(),
273 "soongZipFlags": soongZipFlags,
274 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800275 },
276 })
277
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000278 if uncompressDex {
279 TransformZipAlign(ctx, output, tmpOutput)
280 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800281}