blob: 04b8fc689e6dc75ff8c43271aa6d697def87bdf6 [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 (
Colin Cross8faf8fc2019-01-16 15:15:52 -080018 "github.com/google/blueprint"
19
20 "android/soong/android"
21)
22
23var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
Andrei Onea23fea042020-08-12 16:48:23 +010024 Command: "${config.Class2NonSdkList} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
25 CommandDeps: []string{"${config.Class2NonSdkList}"},
David Brazdil0f670a22019-01-18 16:30:03 +000026}, "outFlag", "stubAPIFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -080027
Colin Crossf24a22a2019-01-31 14:12:44 -080028type hiddenAPI struct {
Paul Duffinf75e5272021-02-09 14:34:25 +000029 // True if the module containing this structure contributes to the hiddenapi information or has
30 // that information encoded within it.
Paul Duffin4103e922021-02-01 19:01:34 +000031 active bool
32
Paul Duffinff774a02021-01-29 12:53:15 +000033 // The path to the dex jar that is in the boot class path. If this is nil then the associated
34 // module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional
35 // annotations for the <x> boot dex jar but which do not actually provide a boot dex jar
36 // themselves.
Paul Duffin4103e922021-02-01 19:01:34 +000037 //
38 // This must be the path to the unencoded dex jar as the encoded dex jar indirectly depends on
39 // 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 +000040 bootDexJarPath android.Path
41
Paul Duffin36187b22021-04-22 16:43:06 +010042 // The paths to the classes jars that contain classes and class members annotated with
43 // the UnsupportedAppUsage annotation that need to be extracted as part of the hidden API
44 // processing.
45 classesJarPaths android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -080046}
47
Colin Crossf24a22a2019-01-31 14:12:44 -080048func (h *hiddenAPI) bootDexJar() android.Path {
49 return h.bootDexJarPath
50}
51
Paul Duffin36187b22021-04-22 16:43:06 +010052func (h *hiddenAPI) classesJars() android.Paths {
53 return h.classesJarPaths
54}
55
Paul Duffin537ea3d2021-05-14 10:38:00 +010056// hiddenAPIModule is the interface a module that embeds the hiddenAPI structure must implement.
57type hiddenAPIModule interface {
58 android.Module
59 hiddenAPIIntf
60}
61
Colin Crossf24a22a2019-01-31 14:12:44 -080062type hiddenAPIIntf interface {
Colin Crossf24a22a2019-01-31 14:12:44 -080063 bootDexJar() android.Path
Paul Duffin36187b22021-04-22 16:43:06 +010064 classesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -080065}
66
67var _ hiddenAPIIntf = (*hiddenAPI)(nil)
68
Paul Duffin4103e922021-02-01 19:01:34 +000069// Initialize the hiddenapi structure
Paul Duffin74d18d12021-05-14 14:18:47 +010070func (h *hiddenAPI) initHiddenAPI(ctx android.ModuleContext, dexJar, classesJar android.Path) {
71
72 // Save the classes jars even if this is not active as they may be used by modular hidden API
73 // processing.
74 classesJars := android.Paths{classesJar}
75 ctx.VisitDirectDepsWithTag(hiddenApiAnnotationsTag, func(dep android.Module) {
76 javaInfo := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
77 classesJars = append(classesJars, javaInfo.ImplementationJars...)
78 })
79 h.classesJarPaths = classesJars
80
81 // Save the unencoded dex jar so it can be used when generating the
82 // hiddenAPISingletonPathsStruct.stubFlags file.
83 h.bootDexJarPath = dexJar
84
Paul Duffin4103e922021-02-01 19:01:34 +000085 // If hiddenapi processing is disabled treat this as inactive.
86 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
87 return
88 }
89
Paul Duffin74d18d12021-05-14 14:18:47 +010090 // The context module must implement hiddenAPIModule.
91 module := ctx.Module().(hiddenAPIModule)
92
Paul Duffinb6f53c02021-05-14 07:52:42 +010093 // If the frameworks/base directories does not exist and no prebuilt hidden API flag files have
94 // been configured then it is not possible to do hidden API encoding.
95 if !ctx.Config().FrameworksBaseDirExists(ctx) && ctx.Config().PrebuiltHiddenApiDir(ctx) == "" {
96 return
97 }
98
Paul Duffin4103e922021-02-01 19:01:34 +000099 // It is important that hiddenapi information is only gathered for/from modules that are actually
100 // on the boot jars list because the runtime only enforces access to the hidden API for the
101 // bootclassloader. If information is gathered for modules not on the list then that will cause
102 // failures in the CtsHiddenApiBlocklist... tests.
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000103 h.active = isModuleInBootClassPath(ctx, module)
Paul Duffin4103e922021-02-01 19:01:34 +0000104}
105
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000106func isModuleInBootClassPath(ctx android.BaseModuleContext, module android.Module) bool {
107 // Get the configured non-updatable and updatable boot jars.
108 nonUpdatableBootJars := ctx.Config().NonUpdatableBootJars()
109 updatableBootJars := ctx.Config().UpdatableBootJars()
110 active := isModuleInConfiguredList(ctx, module, nonUpdatableBootJars) ||
111 isModuleInConfiguredList(ctx, module, updatableBootJars)
112 return active
113}
114
Paul Duffinafaa47c2021-05-14 13:04:04 +0100115// hiddenAPIEncodeDex is called by any module that needs to encode dex files.
Paul Duffin4103e922021-02-01 19:01:34 +0000116//
117// It ignores any module that has not had initHiddenApi() called on it and which is not in the boot
Paul Duffinafaa47c2021-05-14 13:04:04 +0100118// jar list. In that case it simply returns the supplied dex jar path.
Paul Duffin4103e922021-02-01 19:01:34 +0000119//
Paul Duffinafaa47c2021-05-14 13:04:04 +0100120// Otherwise, it creates a copy of the supplied dex file into which it has encoded the hiddenapi
121// flags and returns this instead of the supplied dex jar.
122func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath, uncompressDex bool) android.OutputPath {
Paul Duffin001e6062021-05-14 01:13:55 +0100123
Paul Duffin4103e922021-02-01 19:01:34 +0000124 if !h.active {
125 return dexJar
126 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000127
Paul Duffin66cdbf02021-05-14 16:35:06 +0100128 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", dexJar.Base()).OutputPath
Paul Duffina2058f82020-06-24 16:22:38 +0100129
Paul Duffinf8f4af82021-02-12 15:42:20 +0000130 // Create a copy of the dex jar which has been encoded with hiddenapi flags.
131 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
Paul Duffin4103e922021-02-01 19:01:34 +0000132
Paul Duffinf8f4af82021-02-12 15:42:20 +0000133 // Use the encoded dex jar from here onwards.
134 dexJar = hiddenAPIJar
Colin Crossf24a22a2019-01-31 14:12:44 -0800135
136 return dexJar
137}
138
Paul Duffin850e61f2021-05-14 09:58:48 +0100139// buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file
140// from the classes jars and stub-flags.csv files.
Paul Duffinafaa47c2021-05-14 13:04:04 +0100141//
142// The annotation-flags.csv file contains mappings from Java signature to various flags derived from
143// annotations in the source, e.g. whether it is public or the sdk version above which it can no
144// longer be used.
145//
146// It is created by the Class2NonSdkList tool which processes the .class files in the class
147// implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
148// tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
149// consistency checks on the information in the annotations and to filter out bridge methods
150// that are already part of the public API.
Paul Duffin850e61f2021-05-14 09:58:48 +0100151func buildRuleToGenerateAnnotationFlags(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, outputPath android.WritablePath) {
Colin Cross8faf8fc2019-01-16 15:15:52 -0800152 ctx.Build(pctx, android.BuildParams{
153 Rule: hiddenAPIGenerateCSVRule,
Paul Duffin850e61f2021-05-14 09:58:48 +0100154 Description: desc,
Paul Duffin031d8692021-02-12 11:46:42 +0000155 Inputs: classesJars,
Paul Duffin850e61f2021-05-14 09:58:48 +0100156 Output: outputPath,
David Brazdil0f670a22019-01-18 16:30:03 +0000157 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800158 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000159 "outFlag": "--write-flags-csv",
160 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800161 },
162 })
Paul Duffin850e61f2021-05-14 09:58:48 +0100163}
Colin Cross8faf8fc2019-01-16 15:15:52 -0800164
Paul Duffin850e61f2021-05-14 09:58:48 +0100165// buildRuleToGenerateMetadata builds a ninja rule to generate the metadata.csv file from
166// the classes jars and stub-flags.csv files.
Paul Duffinafaa47c2021-05-14 13:04:04 +0100167//
168// The metadata.csv file contains mappings from Java signature to the value of properties specified
169// on UnsupportedAppUsage annotations in the source.
170//
171// Like the annotation-flags.csv file this is also created by the Class2NonSdkList in the same way.
172// Although the two files could potentially be created in a single invocation of the
173// Class2NonSdkList at the moment they are created using their own invocation, with the behavior
174// being determined by the property that is used.
Paul Duffin850e61f2021-05-14 09:58:48 +0100175func buildRuleToGenerateMetadata(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, metadataCSV android.WritablePath) {
Colin Cross8faf8fc2019-01-16 15:15:52 -0800176 ctx.Build(pctx, android.BuildParams{
177 Rule: hiddenAPIGenerateCSVRule,
Paul Duffin850e61f2021-05-14 09:58:48 +0100178 Description: desc,
Paul Duffin031d8692021-02-12 11:46:42 +0000179 Inputs: classesJars,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800180 Output: metadataCSV,
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-metadata-csv",
184 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800185 },
186 })
Paul Duffin850e61f2021-05-14 09:58:48 +0100187}
Colin Cross8faf8fc2019-01-16 15:15:52 -0800188
Paul Duffinafaa47c2021-05-14 13:04:04 +0100189// buildRuleToGenerateIndex builds a ninja rule to generate the index.csv file from the classes
Paul Duffin850e61f2021-05-14 09:58:48 +0100190// jars.
Paul Duffinafaa47c2021-05-14 13:04:04 +0100191//
192// The index.csv file contains mappings from Java signature to source location information.
193//
194// It is created by the merge_csv tool which processes the class implementation jar, extracting
195// all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
196// created by the unsupported app usage annotation processor during compilation of the class
197// implementation jar.
Paul Duffin850e61f2021-05-14 09:58:48 +0100198func buildRuleToGenerateIndex(ctx android.ModuleContext, desc string, classesJars android.Paths, indexCSV android.WritablePath) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800199 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000200 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800201 BuiltTool("merge_csv").
Paul Duffin031d8692021-02-12 11:46:42 +0000202 Flag("--zip_input").
Paul Duffin2c36f242021-02-16 16:57:06 +0000203 Flag("--key_field signature").
Paul Duffin537ea3d2021-05-14 10:38:00 +0100204 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
Paul Duffin031d8692021-02-12 11:46:42 +0000205 FlagWithOutput("--output=", indexCSV).
206 Inputs(classesJars)
Paul Duffin850e61f2021-05-14 09:58:48 +0100207 rule.Build(desc, desc)
Colin Cross8faf8fc2019-01-16 15:15:52 -0800208}
209
210var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayevb5df8a02020-02-19 16:39:59 +0000211 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
Colin Crossd783bbb2020-07-11 22:30:45 -0700212 unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
Artur Satayevb5df8a02020-02-19 16:39:59 +0000213 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
214 echo "--input-dex=$${INPUT_DEX}";
215 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
216 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
217 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
218 ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800219 CommandDeps: []string{
220 "${config.HiddenAPI}",
221 "${config.SoongZipCmd}",
222 "${config.MergeZipsCmd}",
223 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000224}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800225
Colin Crossf24a22a2019-01-31 14:12:44 -0800226func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800227 uncompressDex bool) {
228
Colin Crossf24a22a2019-01-31 14:12:44 -0800229 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800230
Colin Crosscd964b32019-01-18 22:03:02 -0800231 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
232 // in the input it stays uncompressed in the output.
233 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000234 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000235 tmpOutput := output
236 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800237 if uncompressDex {
238 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000239 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
240 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800241 }
Jiyong Park93e57a02020-02-21 16:04:53 +0900242
243 enforceHiddenApiFlagsToAllMembers := true
Paul Duffinb6f53c02021-05-14 07:52:42 +0100244
Jiyong Park93e57a02020-02-21 16:04:53 +0900245 // b/149353192: when a module is instrumented, jacoco adds synthetic members
246 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
247 // don't complain when we don't find hidden API flags for the synthetic members.
Paul Duffinc495d2b2020-05-19 21:07:52 +0100248 if j, ok := ctx.Module().(interface {
249 shouldInstrument(android.BaseModuleContext) bool
250 }); ok && j.shouldInstrument(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900251 enforceHiddenApiFlagsToAllMembers = false
252 }
253
254 if !enforceHiddenApiFlagsToAllMembers {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000255 hiddenapiFlags = "--no-force-assign-all"
256 }
Colin Crosscd964b32019-01-18 22:03:02 -0800257
Colin Cross8faf8fc2019-01-16 15:15:52 -0800258 ctx.Build(pctx, android.BuildParams{
259 Rule: hiddenAPIEncodeDexRule,
260 Description: "hiddenapi encode dex",
261 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000262 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800263 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800264 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800265 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000266 "tmpDir": tmpDir.String(),
267 "soongZipFlags": soongZipFlags,
268 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800269 },
270 })
271
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000272 if uncompressDex {
273 TransformZipAlign(ctx, output, tmpOutput)
274 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800275}
Paul Duffin031d8692021-02-12 11:46:42 +0000276
277type hiddenApiAnnotationsDependencyTag struct {
278 blueprint.BaseDependencyTag
279}
280
281// Tag used to mark dependencies on java_library instances that contains Java source files whose
282// sole purpose is to provide additional hiddenapi annotations.
283var hiddenApiAnnotationsTag hiddenApiAnnotationsDependencyTag
284
285// Mark this tag so dependencies that use it are excluded from APEX contents.
286func (t hiddenApiAnnotationsDependencyTag) ExcludeFromApexContents() {}
287
288var _ android.ExcludeFromApexContentsTag = hiddenApiAnnotationsTag