blob: 069595eb9a2131fd2b3e666d0d718bb01f854997 [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
Paul Duffin4fd997b2021-02-03 20:06:33 +0000126// hiddenAPIExtractAndEncode is called by any module that could contribute to the hiddenapi
127// processing.
Paul Duffin4103e922021-02-01 19:01:34 +0000128//
129// It ignores any module that has not had initHiddenApi() called on it and which is not in the boot
130// jar list.
131//
132// Otherwise, it generates ninja rules to do the following:
Paul Duffin4fd997b2021-02-03 20:06:33 +0000133// 1. Extract information needed for hiddenapi processing from the module and output it into CSV
134// files.
Paul Duffin4103e922021-02-01 19:01:34 +0000135// 2. Conditionally adds the supplied dex file to the list of files used to generate the
136// hiddenAPISingletonPathsStruct.stubsFlag file.
137// 3. Conditionally creates a copy of the supplied dex file into which it has encoded the hiddenapi
138// flags and returns this instead of the supplied dex jar, otherwise simply returns the supplied
139// dex jar.
Paul Duffin4fd997b2021-02-03 20:06:33 +0000140func (h *hiddenAPI) hiddenAPIExtractAndEncode(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath,
Paul Duffin612e6102021-02-02 13:38:13 +0000141 implementationJar android.Path, uncompressDex bool) android.OutputPath {
Paul Duffind2aceca2019-02-28 16:13:20 +0000142
Paul Duffin4103e922021-02-01 19:01:34 +0000143 if !h.active {
144 return dexJar
145 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000146
Paul Duffin9d67ca62021-02-03 20:06:33 +0000147 h.hiddenAPIExtractInformation(ctx, dexJar, implementationJar, primary)
Paul Duffind2aceca2019-02-28 16:13:20 +0000148
Paul Duffin4103e922021-02-01 19:01:34 +0000149 if !h.annotationsOnly {
150 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar").OutputPath
Paul Duffina2058f82020-06-24 16:22:38 +0100151
Paul Duffin4103e922021-02-01 19:01:34 +0000152 // Create a copy of the dex jar which has been encoded with hiddenapi flags.
153 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
154
155 // Use the encoded dex jar from here onwards.
156 dexJar = hiddenAPIJar
Colin Crossf24a22a2019-01-31 14:12:44 -0800157 }
158
159 return dexJar
160}
161
Paul Duffin4fd997b2021-02-03 20:06:33 +0000162// hiddenAPIExtractInformation generates ninja rules to extract the information from the classes
163// jar, and outputs it to the appropriate module specific CSV file.
164//
165// It also makes the dex jar available for use when generating the
166// hiddenAPISingletonPathsStruct.stubFlags.
Paul Duffin9d67ca62021-02-03 20:06:33 +0000167func (h *hiddenAPI) hiddenAPIExtractInformation(ctx android.ModuleContext, dexJar, classesJar android.Path, primary bool) {
168 if !h.active {
169 return
170 }
171
172 // More than one library with the same classes may need to be encoded but only one should be
173 // used as a source of information for hidden API processing otherwise it will result in
174 // duplicate entries in the files.
175 if !primary {
176 return
177 }
178
Colin Crossf24a22a2019-01-31 14:12:44 -0800179 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800180
Paul Duffin34982f12021-01-27 15:11:42 +0000181 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800182 ctx.Build(pctx, android.BuildParams{
183 Rule: hiddenAPIGenerateCSVRule,
184 Description: "hiddenapi flags",
185 Input: classesJar,
186 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000187 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800188 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000189 "outFlag": "--write-flags-csv",
190 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800191 },
192 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000193 h.flagsCSVPath = flagsCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800194
Paul Duffin34982f12021-01-27 15:11:42 +0000195 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800196 ctx.Build(pctx, android.BuildParams{
197 Rule: hiddenAPIGenerateCSVRule,
198 Description: "hiddenapi metadata",
199 Input: classesJar,
200 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000201 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800202 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000203 "outFlag": "--write-metadata-csv",
204 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800205 },
206 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000207 h.metadataCSVPath = metadataCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800208
Paul Duffin34982f12021-01-27 15:11:42 +0000209 indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
Colin Crossf1a035e2020-11-16 17:32:30 -0800210 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000211 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800212 BuiltTool("merge_csv").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000213 FlagWithInput("--zip_input=", classesJar).
214 FlagWithOutput("--output=", indexCSV)
Colin Crossf1a035e2020-11-16 17:32:30 -0800215 rule.Build("merged-hiddenapi-index", "Merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000216 h.indexCSVPath = indexCSV
Paul Duffin4fd997b2021-02-03 20:06:33 +0000217
218 // Save the unencoded dex jar so it can be used when generating the
219 // hiddenAPISingletonPathsStruct.stubFlags file.
220 h.bootDexJarPath = dexJar
Colin Cross8faf8fc2019-01-16 15:15:52 -0800221}
222
223var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayevb5df8a02020-02-19 16:39:59 +0000224 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
Colin Crossd783bbb2020-07-11 22:30:45 -0700225 unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
Artur Satayevb5df8a02020-02-19 16:39:59 +0000226 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
227 echo "--input-dex=$${INPUT_DEX}";
228 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
229 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
230 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
231 ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800232 CommandDeps: []string{
233 "${config.HiddenAPI}",
234 "${config.SoongZipCmd}",
235 "${config.MergeZipsCmd}",
236 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000237}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800238
Colin Crossf24a22a2019-01-31 14:12:44 -0800239func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800240 uncompressDex bool) {
241
Colin Crossf24a22a2019-01-31 14:12:44 -0800242 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800243
Colin Crosscd964b32019-01-18 22:03:02 -0800244 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
245 // in the input it stays uncompressed in the output.
246 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000247 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000248 tmpOutput := output
249 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800250 if uncompressDex {
251 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000252 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
253 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800254 }
Jiyong Park93e57a02020-02-21 16:04:53 +0900255
256 enforceHiddenApiFlagsToAllMembers := true
David Brazdil91b4e3e2019-01-23 21:04:05 +0000257 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
258 // Disable assertion that all methods/fields have hidden API flags assigned.
259 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900260 enforceHiddenApiFlagsToAllMembers = false
261 }
262 // b/149353192: when a module is instrumented, jacoco adds synthetic members
263 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
264 // don't complain when we don't find hidden API flags for the synthetic members.
Paul Duffinc495d2b2020-05-19 21:07:52 +0100265 if j, ok := ctx.Module().(interface {
266 shouldInstrument(android.BaseModuleContext) bool
267 }); ok && j.shouldInstrument(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900268 enforceHiddenApiFlagsToAllMembers = false
269 }
270
271 if !enforceHiddenApiFlagsToAllMembers {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000272 hiddenapiFlags = "--no-force-assign-all"
273 }
Colin Crosscd964b32019-01-18 22:03:02 -0800274
Colin Cross8faf8fc2019-01-16 15:15:52 -0800275 ctx.Build(pctx, android.BuildParams{
276 Rule: hiddenAPIEncodeDexRule,
277 Description: "hiddenapi encode dex",
278 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000279 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800280 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800281 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800282 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000283 "tmpDir": tmpDir.String(),
284 "soongZipFlags": soongZipFlags,
285 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800286 },
287 })
288
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000289 if uncompressDex {
290 TransformZipAlign(ctx, output, tmpOutput)
291 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800292}