blob: 3af5f1c7b780c51353d07d71c85932b4a5aa9a69 [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
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +010033 // The path to the dex jar that is in the boot class path. If this is unset then the associated
Paul Duffinff774a02021-01-29 12:53:15 +000034 // 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.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +010040 bootDexJarPath OptionalDexJarPath
Paul Duffinff774a02021-01-29 12:53:15 +000041
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
Paul Duffin1bbd0622021-05-14 15:52:25 +010046
47 // The compressed state of the dex file being encoded. This is used to ensure that the encoded
48 // dex file has the same state.
49 uncompressDexState *bool
Colin Crossf24a22a2019-01-31 14:12:44 -080050}
51
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +010052func (h *hiddenAPI) bootDexJar() OptionalDexJarPath {
Colin Crossf24a22a2019-01-31 14:12:44 -080053 return h.bootDexJarPath
54}
55
Paul Duffin36187b22021-04-22 16:43:06 +010056func (h *hiddenAPI) classesJars() android.Paths {
57 return h.classesJarPaths
58}
59
Paul Duffin1bbd0622021-05-14 15:52:25 +010060func (h *hiddenAPI) uncompressDex() *bool {
61 return h.uncompressDexState
62}
63
Paul Duffin537ea3d2021-05-14 10:38:00 +010064// hiddenAPIModule is the interface a module that embeds the hiddenAPI structure must implement.
65type hiddenAPIModule interface {
66 android.Module
67 hiddenAPIIntf
68}
69
Colin Crossf24a22a2019-01-31 14:12:44 -080070type hiddenAPIIntf interface {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +010071 bootDexJar() OptionalDexJarPath
Paul Duffin36187b22021-04-22 16:43:06 +010072 classesJars() android.Paths
Paul Duffin1bbd0622021-05-14 15:52:25 +010073 uncompressDex() *bool
Colin Crossf24a22a2019-01-31 14:12:44 -080074}
75
76var _ hiddenAPIIntf = (*hiddenAPI)(nil)
77
Paul Duffin4103e922021-02-01 19:01:34 +000078// Initialize the hiddenapi structure
Paul Duffin1bbd0622021-05-14 15:52:25 +010079//
80// uncompressedDexState should be nil when the module is a prebuilt and so does not require hidden
81// API encoding.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +010082func (h *hiddenAPI) initHiddenAPI(ctx android.ModuleContext, dexJar OptionalDexJarPath, classesJar android.Path, uncompressedDexState *bool) {
Paul Duffin74d18d12021-05-14 14:18:47 +010083
84 // Save the classes jars even if this is not active as they may be used by modular hidden API
85 // processing.
86 classesJars := android.Paths{classesJar}
87 ctx.VisitDirectDepsWithTag(hiddenApiAnnotationsTag, func(dep android.Module) {
88 javaInfo := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
89 classesJars = append(classesJars, javaInfo.ImplementationJars...)
90 })
91 h.classesJarPaths = classesJars
92
93 // Save the unencoded dex jar so it can be used when generating the
94 // hiddenAPISingletonPathsStruct.stubFlags file.
95 h.bootDexJarPath = dexJar
96
Paul Duffin1bbd0622021-05-14 15:52:25 +010097 h.uncompressDexState = uncompressedDexState
98
Paul Duffin4103e922021-02-01 19:01:34 +000099 // If hiddenapi processing is disabled treat this as inactive.
100 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
101 return
102 }
103
Paul Duffin74d18d12021-05-14 14:18:47 +0100104 // The context module must implement hiddenAPIModule.
105 module := ctx.Module().(hiddenAPIModule)
106
Paul Duffinb6f53c02021-05-14 07:52:42 +0100107 // If the frameworks/base directories does not exist and no prebuilt hidden API flag files have
108 // been configured then it is not possible to do hidden API encoding.
109 if !ctx.Config().FrameworksBaseDirExists(ctx) && ctx.Config().PrebuiltHiddenApiDir(ctx) == "" {
110 return
111 }
112
Paul Duffin4103e922021-02-01 19:01:34 +0000113 // It is important that hiddenapi information is only gathered for/from modules that are actually
114 // on the boot jars list because the runtime only enforces access to the hidden API for the
115 // bootclassloader. If information is gathered for modules not on the list then that will cause
116 // failures in the CtsHiddenApiBlocklist... tests.
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000117 h.active = isModuleInBootClassPath(ctx, module)
Paul Duffin4103e922021-02-01 19:01:34 +0000118}
119
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000120func isModuleInBootClassPath(ctx android.BaseModuleContext, module android.Module) bool {
satayevd604b212021-07-21 14:23:52 +0100121 // Get the configured platform and apex boot jars.
122 nonApexBootJars := ctx.Config().NonApexBootJars()
123 apexBootJars := ctx.Config().ApexBootJars()
124 active := isModuleInConfiguredList(ctx, module, nonApexBootJars) ||
125 isModuleInConfiguredList(ctx, module, apexBootJars)
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000126 return active
127}
128
Paul Duffinafaa47c2021-05-14 13:04:04 +0100129// hiddenAPIEncodeDex is called by any module that needs to encode dex files.
Paul Duffin4103e922021-02-01 19:01:34 +0000130//
131// 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 +0100132// jar list. In that case it simply returns the supplied dex jar path.
Paul Duffin4103e922021-02-01 19:01:34 +0000133//
Paul Duffinafaa47c2021-05-14 13:04:04 +0100134// Otherwise, it creates a copy of the supplied dex file into which it has encoded the hiddenapi
135// flags and returns this instead of the supplied dex jar.
Paul Duffin1bbd0622021-05-14 15:52:25 +0100136func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath) android.OutputPath {
Paul Duffin001e6062021-05-14 01:13:55 +0100137
Paul Duffin4103e922021-02-01 19:01:34 +0000138 if !h.active {
139 return dexJar
140 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000141
Paul Duffin1bbd0622021-05-14 15:52:25 +0100142 // A nil uncompressDexState prevents the dex file from being encoded.
143 if h.uncompressDexState == nil {
144 ctx.ModuleErrorf("cannot encode dex file %s when uncompressDexState is nil", dexJar)
145 }
146 uncompressDex := *h.uncompressDexState
147
Paul Duffinf8f4af82021-02-12 15:42:20 +0000148 // Create a copy of the dex jar which has been encoded with hiddenapi flags.
Paul Duffin09165952021-05-16 23:32:52 +0100149 flagsCSV := hiddenAPISingletonPaths(ctx).flags
150 outputDir := android.PathForModuleOut(ctx, "hiddenapi").OutputPath
151 encodedDex := hiddenAPIEncodeDex(ctx, dexJar, flagsCSV, uncompressDex, outputDir)
Paul Duffin4103e922021-02-01 19:01:34 +0000152
Paul Duffinf8f4af82021-02-12 15:42:20 +0000153 // Use the encoded dex jar from here onwards.
Paul Duffin09165952021-05-16 23:32:52 +0100154 return encodedDex
Colin Crossf24a22a2019-01-31 14:12:44 -0800155}
156
Paul Duffin850e61f2021-05-14 09:58:48 +0100157// buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file
158// from the classes jars and stub-flags.csv files.
Paul Duffinafaa47c2021-05-14 13:04:04 +0100159//
160// The annotation-flags.csv file contains mappings from Java signature to various flags derived from
161// annotations in the source, e.g. whether it is public or the sdk version above which it can no
162// longer be used.
163//
164// It is created by the Class2NonSdkList tool which processes the .class files in the class
165// implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
166// tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
167// consistency checks on the information in the annotations and to filter out bridge methods
168// that are already part of the public API.
Paul Duffin850e61f2021-05-14 09:58:48 +0100169func buildRuleToGenerateAnnotationFlags(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, outputPath android.WritablePath) {
Colin Cross8faf8fc2019-01-16 15:15:52 -0800170 ctx.Build(pctx, android.BuildParams{
171 Rule: hiddenAPIGenerateCSVRule,
Paul Duffin850e61f2021-05-14 09:58:48 +0100172 Description: desc,
Paul Duffin031d8692021-02-12 11:46:42 +0000173 Inputs: classesJars,
Paul Duffin850e61f2021-05-14 09:58:48 +0100174 Output: outputPath,
David Brazdil0f670a22019-01-18 16:30:03 +0000175 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800176 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000177 "outFlag": "--write-flags-csv",
178 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800179 },
180 })
Paul Duffin850e61f2021-05-14 09:58:48 +0100181}
Colin Cross8faf8fc2019-01-16 15:15:52 -0800182
Paul Duffin850e61f2021-05-14 09:58:48 +0100183// buildRuleToGenerateMetadata builds a ninja rule to generate the metadata.csv file from
184// the classes jars and stub-flags.csv files.
Paul Duffinafaa47c2021-05-14 13:04:04 +0100185//
186// The metadata.csv file contains mappings from Java signature to the value of properties specified
187// on UnsupportedAppUsage annotations in the source.
188//
189// Like the annotation-flags.csv file this is also created by the Class2NonSdkList in the same way.
190// Although the two files could potentially be created in a single invocation of the
191// Class2NonSdkList at the moment they are created using their own invocation, with the behavior
192// being determined by the property that is used.
Paul Duffin850e61f2021-05-14 09:58:48 +0100193func buildRuleToGenerateMetadata(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, metadataCSV android.WritablePath) {
Colin Cross8faf8fc2019-01-16 15:15:52 -0800194 ctx.Build(pctx, android.BuildParams{
195 Rule: hiddenAPIGenerateCSVRule,
Paul Duffin850e61f2021-05-14 09:58:48 +0100196 Description: desc,
Paul Duffin031d8692021-02-12 11:46:42 +0000197 Inputs: classesJars,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800198 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000199 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800200 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000201 "outFlag": "--write-metadata-csv",
202 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800203 },
204 })
Paul Duffin850e61f2021-05-14 09:58:48 +0100205}
Colin Cross8faf8fc2019-01-16 15:15:52 -0800206
Paul Duffinafaa47c2021-05-14 13:04:04 +0100207// buildRuleToGenerateIndex builds a ninja rule to generate the index.csv file from the classes
Paul Duffin850e61f2021-05-14 09:58:48 +0100208// jars.
Paul Duffinafaa47c2021-05-14 13:04:04 +0100209//
210// The index.csv file contains mappings from Java signature to source location information.
211//
212// It is created by the merge_csv tool which processes the class implementation jar, extracting
213// all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
214// created by the unsupported app usage annotation processor during compilation of the class
215// implementation jar.
Paul Duffin850e61f2021-05-14 09:58:48 +0100216func buildRuleToGenerateIndex(ctx android.ModuleContext, desc string, classesJars android.Paths, indexCSV android.WritablePath) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800217 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000218 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800219 BuiltTool("merge_csv").
Paul Duffin031d8692021-02-12 11:46:42 +0000220 Flag("--zip_input").
Paul Duffin2c36f242021-02-16 16:57:06 +0000221 Flag("--key_field signature").
Paul Duffin031d8692021-02-12 11:46:42 +0000222 FlagWithOutput("--output=", indexCSV).
223 Inputs(classesJars)
Paul Duffin850e61f2021-05-14 09:58:48 +0100224 rule.Build(desc, desc)
Colin Cross8faf8fc2019-01-16 15:15:52 -0800225}
226
227var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayevb5df8a02020-02-19 16:39:59 +0000228 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
Colin Crossd783bbb2020-07-11 22:30:45 -0700229 unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
Artur Satayevb5df8a02020-02-19 16:39:59 +0000230 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
231 echo "--input-dex=$${INPUT_DEX}";
232 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
233 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
234 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
Paul Duffin4de94502021-05-16 05:21:16 +0100235 ${config.MergeZipsCmd} -j -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800236 CommandDeps: []string{
237 "${config.HiddenAPI}",
238 "${config.SoongZipCmd}",
239 "${config.MergeZipsCmd}",
240 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000241}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800242
Paul Duffin09165952021-05-16 23:32:52 +0100243// hiddenAPIEncodeDex generates the build rule that will encode the supplied dex jar and place the
244// encoded dex jar in a file of the same name in the output directory.
245//
246// The encode dex rule requires unzipping, encoding and rezipping the classes.dex files along with
247// all the resources from the input jar. It also ensures that if it was uncompressed in the input
248// it stays uncompressed in the output.
249func hiddenAPIEncodeDex(ctx android.ModuleContext, dexInput, flagsCSV android.Path, uncompressDex bool, outputDir android.OutputPath) android.OutputPath {
Colin Crosscd964b32019-01-18 22:03:02 -0800250
Paul Duffin09165952021-05-16 23:32:52 +0100251 // The output file has the same name as the input file and is in the output directory.
252 output := outputDir.Join(ctx, dexInput.Base())
Colin Cross8faf8fc2019-01-16 15:15:52 -0800253
Paul Duffin09165952021-05-16 23:32:52 +0100254 // Create a jar specific temporary directory in which to do the work just in case this is called
255 // with the same output directory for multiple modules.
256 tmpDir := outputDir.Join(ctx, dexInput.Base()+"-tmp")
257
258 // If the input is uncompressed then generate the output of the encode rule to an intermediate
259 // file as the final output will need further processing after encoding.
Colin Crosscd964b32019-01-18 22:03:02 -0800260 soongZipFlags := ""
Paul Duffin09165952021-05-16 23:32:52 +0100261 encodeRuleOutput := output
Colin Crosscd964b32019-01-18 22:03:02 -0800262 if uncompressDex {
263 soongZipFlags = "-L 0"
Paul Duffin09165952021-05-16 23:32:52 +0100264 encodeRuleOutput = outputDir.Join(ctx, "unaligned", dexInput.Base())
Colin Crosscd964b32019-01-18 22:03:02 -0800265 }
Jiyong Park93e57a02020-02-21 16:04:53 +0900266
Jiyong Park93e57a02020-02-21 16:04:53 +0900267 // b/149353192: when a module is instrumented, jacoco adds synthetic members
268 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
269 // don't complain when we don't find hidden API flags for the synthetic members.
Paul Duffin09165952021-05-16 23:32:52 +0100270 hiddenapiFlags := ""
Paul Duffinc495d2b2020-05-19 21:07:52 +0100271 if j, ok := ctx.Module().(interface {
272 shouldInstrument(android.BaseModuleContext) bool
273 }); ok && j.shouldInstrument(ctx) {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000274 hiddenapiFlags = "--no-force-assign-all"
275 }
Colin Crosscd964b32019-01-18 22:03:02 -0800276
Colin Cross8faf8fc2019-01-16 15:15:52 -0800277 ctx.Build(pctx, android.BuildParams{
278 Rule: hiddenAPIEncodeDexRule,
279 Description: "hiddenapi encode dex",
280 Input: dexInput,
Paul Duffin09165952021-05-16 23:32:52 +0100281 Output: encodeRuleOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800282 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800283 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800284 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000285 "tmpDir": tmpDir.String(),
286 "soongZipFlags": soongZipFlags,
287 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800288 },
289 })
290
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000291 if uncompressDex {
Paul Duffin09165952021-05-16 23:32:52 +0100292 TransformZipAlign(ctx, output, encodeRuleOutput)
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000293 }
Paul Duffin09165952021-05-16 23:32:52 +0100294
295 return output
Colin Cross8faf8fc2019-01-16 15:15:52 -0800296}
Paul Duffin031d8692021-02-12 11:46:42 +0000297
298type hiddenApiAnnotationsDependencyTag struct {
299 blueprint.BaseDependencyTag
Colin Crossce564252022-01-12 11:13:32 -0800300 android.LicenseAnnotationSharedDependencyTag
Paul Duffin031d8692021-02-12 11:46:42 +0000301}
302
303// Tag used to mark dependencies on java_library instances that contains Java source files whose
304// sole purpose is to provide additional hiddenapi annotations.
305var hiddenApiAnnotationsTag hiddenApiAnnotationsDependencyTag
306
307// Mark this tag so dependencies that use it are excluded from APEX contents.
308func (t hiddenApiAnnotationsDependencyTag) ExcludeFromApexContents() {}
309
310var _ android.ExcludeFromApexContentsTag = hiddenApiAnnotationsTag