blob: 66d9ca050f585ed6c161d0aee3b051174ca1b5ba [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 Duffin4103e922021-02-01 19:01:34 +0000147 // More than one library with the same classes may need to be encoded but only one should be
148 // used as a source of information for hidden API processing otherwise it will result in
149 // duplicate entries in the files.
150 if primary {
Paul Duffin4fd997b2021-02-03 20:06:33 +0000151 h.hiddenAPIExtractInformation(ctx, dexJar, implementationJar)
Paul Duffin4103e922021-02-01 19:01:34 +0000152 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000153
Paul Duffin4103e922021-02-01 19:01:34 +0000154 if !h.annotationsOnly {
155 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar").OutputPath
Paul Duffina2058f82020-06-24 16:22:38 +0100156
Paul Duffin4103e922021-02-01 19:01:34 +0000157 // Create a copy of the dex jar which has been encoded with hiddenapi flags.
158 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
159
160 // Use the encoded dex jar from here onwards.
161 dexJar = hiddenAPIJar
Colin Crossf24a22a2019-01-31 14:12:44 -0800162 }
163
164 return dexJar
165}
166
Paul Duffin4fd997b2021-02-03 20:06:33 +0000167// hiddenAPIExtractInformation generates ninja rules to extract the information from the classes
168// jar, and outputs it to the appropriate module specific CSV file.
169//
170// It also makes the dex jar available for use when generating the
171// hiddenAPISingletonPathsStruct.stubFlags.
172func (h *hiddenAPI) hiddenAPIExtractInformation(ctx android.ModuleContext, dexJar, classesJar android.Path) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800173 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800174
Paul Duffin34982f12021-01-27 15:11:42 +0000175 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800176 ctx.Build(pctx, android.BuildParams{
177 Rule: hiddenAPIGenerateCSVRule,
178 Description: "hiddenapi flags",
179 Input: classesJar,
180 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000181 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800182 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000183 "outFlag": "--write-flags-csv",
184 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800185 },
186 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000187 h.flagsCSVPath = flagsCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800188
Paul Duffin34982f12021-01-27 15:11:42 +0000189 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800190 ctx.Build(pctx, android.BuildParams{
191 Rule: hiddenAPIGenerateCSVRule,
192 Description: "hiddenapi metadata",
193 Input: classesJar,
194 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000195 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800196 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000197 "outFlag": "--write-metadata-csv",
198 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800199 },
200 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000201 h.metadataCSVPath = metadataCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800202
Paul Duffin34982f12021-01-27 15:11:42 +0000203 indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
Colin Crossf1a035e2020-11-16 17:32:30 -0800204 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000205 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800206 BuiltTool("merge_csv").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000207 FlagWithInput("--zip_input=", classesJar).
208 FlagWithOutput("--output=", indexCSV)
Colin Crossf1a035e2020-11-16 17:32:30 -0800209 rule.Build("merged-hiddenapi-index", "Merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000210 h.indexCSVPath = indexCSV
Paul Duffin4fd997b2021-02-03 20:06:33 +0000211
212 // Save the unencoded dex jar so it can be used when generating the
213 // hiddenAPISingletonPathsStruct.stubFlags file.
214 h.bootDexJarPath = dexJar
Colin Cross8faf8fc2019-01-16 15:15:52 -0800215}
216
217var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayevb5df8a02020-02-19 16:39:59 +0000218 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
Colin Crossd783bbb2020-07-11 22:30:45 -0700219 unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
Artur Satayevb5df8a02020-02-19 16:39:59 +0000220 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
221 echo "--input-dex=$${INPUT_DEX}";
222 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
223 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
224 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
225 ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800226 CommandDeps: []string{
227 "${config.HiddenAPI}",
228 "${config.SoongZipCmd}",
229 "${config.MergeZipsCmd}",
230 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000231}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800232
Colin Crossf24a22a2019-01-31 14:12:44 -0800233func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800234 uncompressDex bool) {
235
Colin Crossf24a22a2019-01-31 14:12:44 -0800236 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800237
Colin Crosscd964b32019-01-18 22:03:02 -0800238 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
239 // in the input it stays uncompressed in the output.
240 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000241 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000242 tmpOutput := output
243 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800244 if uncompressDex {
245 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000246 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
247 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800248 }
Jiyong Park93e57a02020-02-21 16:04:53 +0900249
250 enforceHiddenApiFlagsToAllMembers := true
David Brazdil91b4e3e2019-01-23 21:04:05 +0000251 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
252 // Disable assertion that all methods/fields have hidden API flags assigned.
253 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900254 enforceHiddenApiFlagsToAllMembers = false
255 }
256 // b/149353192: when a module is instrumented, jacoco adds synthetic members
257 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
258 // don't complain when we don't find hidden API flags for the synthetic members.
Paul Duffinc495d2b2020-05-19 21:07:52 +0100259 if j, ok := ctx.Module().(interface {
260 shouldInstrument(android.BaseModuleContext) bool
261 }); ok && j.shouldInstrument(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900262 enforceHiddenApiFlagsToAllMembers = false
263 }
264
265 if !enforceHiddenApiFlagsToAllMembers {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000266 hiddenapiFlags = "--no-force-assign-all"
267 }
Colin Crosscd964b32019-01-18 22:03:02 -0800268
Colin Cross8faf8fc2019-01-16 15:15:52 -0800269 ctx.Build(pctx, android.BuildParams{
270 Rule: hiddenAPIEncodeDexRule,
271 Description: "hiddenapi encode dex",
272 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000273 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800274 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800275 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800276 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000277 "tmpDir": tmpDir.String(),
278 "soongZipFlags": soongZipFlags,
279 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800280 },
281 })
282
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000283 if uncompressDex {
284 TransformZipAlign(ctx, output, tmpOutput)
285 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800286}