blob: 2cd025e4331c580779c6409dc01e989ddaeb5906 [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 Duffinff774a02021-01-29 12:53:15 +000031 // The path to the dex jar that is in the boot class path. If this is nil then the associated
32 // module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional
33 // annotations for the <x> boot dex jar but which do not actually provide a boot dex jar
34 // themselves.
35 bootDexJarPath android.Path
36
37 // The path to the CSV file that contains mappings from Java signature to various flags derived
38 // from annotations in the source, e.g. whether it is public or the sdk version above which it
39 // can no longer be used.
40 //
41 // It is created by the Class2NonSdkList tool which processes the .class files in the class
42 // implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
43 // tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
44 // consistency checks on the information in the annotations and to filter out bridge methods
45 // that are already part of the public API.
46 flagsCSVPath android.Path
47
48 // The path to the CSV file that contains mappings from Java signature to the value of properties
49 // specified on UnsupportedAppUsage annotations in the source.
50 //
51 // Like the flagsCSVPath file this is also created by the Class2NonSdkList in the same way.
52 // Although the two files could potentially be created in a single invocation of the
53 // Class2NonSdkList at the moment they are created using their own invocation, with the behavior
54 // being determined by the property that is used.
Artur Satayevb5df8a02020-02-19 16:39:59 +000055 metadataCSVPath android.Path
Paul Duffinff774a02021-01-29 12:53:15 +000056
57 // The path to the CSV file that contains mappings from Java signature to source location
58 // information.
59 //
60 // It is created by the merge_csv tool which processes the class implementation jar, extracting
61 // all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
62 // created by the unsupported app usage annotation processor during compilation of the class
63 // implementation jar.
64 indexCSVPath android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080065}
66
67func (h *hiddenAPI) flagsCSV() android.Path {
68 return h.flagsCSVPath
69}
70
71func (h *hiddenAPI) metadataCSV() android.Path {
72 return h.metadataCSVPath
73}
74
75func (h *hiddenAPI) bootDexJar() android.Path {
76 return h.bootDexJarPath
77}
78
Artur Satayevb5df8a02020-02-19 16:39:59 +000079func (h *hiddenAPI) indexCSV() android.Path {
80 return h.indexCSVPath
81}
82
Colin Crossf24a22a2019-01-31 14:12:44 -080083type hiddenAPIIntf interface {
Colin Crossf24a22a2019-01-31 14:12:44 -080084 bootDexJar() android.Path
Artur Satayevb5df8a02020-02-19 16:39:59 +000085 flagsCSV() android.Path
86 indexCSV() android.Path
87 metadataCSV() android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080088}
89
90var _ hiddenAPIIntf = (*hiddenAPI)(nil)
91
Paul Duffina2058f82020-06-24 16:22:38 +010092func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bool, dexJar android.ModuleOutPath,
Artur Satayevb5df8a02020-02-19 16:39:59 +000093 implementationJar android.Path, uncompressDex bool) android.ModuleOutPath {
Colin Crossf24a22a2019-01-31 14:12:44 -080094 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
Paul Duffind2aceca2019-02-28 16:13:20 +000095
96 // Modules whose names are of the format <x>-hiddenapi provide hiddenapi information
97 // for the boot jar module <x>. Otherwise, the module provides information for itself.
98 // Either way extract the name of the boot jar module.
99 bootJarName := strings.TrimSuffix(name, "-hiddenapi")
100
101 // If this module is on the boot jars list (or providing information for a module
102 // on the list) then extract the hiddenapi information from it, and if necessary
103 // encode that information in the generated dex file.
104 //
105 // It is important that hiddenapi information is only gathered for/from modules on
106 // that are actually on the boot jars list because the runtime only enforces access
107 // to the hidden API for the bootclassloader. If information is gathered for modules
108 // not on the list then that will cause failures in the CtsHiddenApiBlacklist...
109 // tests.
110 if inList(bootJarName, ctx.Config().BootJars()) {
Paul Duffin34982f12021-01-27 15:11:42 +0000111 // Create ninja rules to generate various CSV files needed by hiddenapi and store the paths
112 // in the hiddenAPI structure.
113 h.hiddenAPIGenerateCSV(ctx, implementationJar)
Paul Duffind2aceca2019-02-28 16:13:20 +0000114
115 // If this module is actually on the boot jars list and not providing
116 // hiddenapi information for a module on the boot jars list then encode
117 // the gathered information in the generated dex file.
118 if name == bootJarName {
119 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar")
Paul Duffina2058f82020-06-24 16:22:38 +0100120
121 // More than one library with the same classes can be encoded but only one can
122 // be added to the global set of flags, otherwise it will result in duplicate
123 // classes which is an error. Therefore, only add the dex jar of one of them
124 // to the global set of flags.
125 if primary {
126 h.bootDexJarPath = dexJar
127 }
Paul Duffind2aceca2019-02-28 16:13:20 +0000128 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
129 dexJar = hiddenAPIJar
130 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800131 }
132 }
133
134 return dexJar
135}
136
Paul Duffin34982f12021-01-27 15:11:42 +0000137func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800138 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800139
Paul Duffin34982f12021-01-27 15:11:42 +0000140 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800141 ctx.Build(pctx, android.BuildParams{
142 Rule: hiddenAPIGenerateCSVRule,
143 Description: "hiddenapi flags",
144 Input: classesJar,
145 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000146 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800147 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000148 "outFlag": "--write-flags-csv",
149 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800150 },
151 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000152 h.flagsCSVPath = flagsCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800153
Paul Duffin34982f12021-01-27 15:11:42 +0000154 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800155 ctx.Build(pctx, android.BuildParams{
156 Rule: hiddenAPIGenerateCSVRule,
157 Description: "hiddenapi metadata",
158 Input: classesJar,
159 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000160 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800161 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000162 "outFlag": "--write-metadata-csv",
163 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800164 },
165 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000166 h.metadataCSVPath = metadataCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800167
Paul Duffin34982f12021-01-27 15:11:42 +0000168 indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
Colin Crossf1a035e2020-11-16 17:32:30 -0800169 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000170 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800171 BuiltTool("merge_csv").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000172 FlagWithInput("--zip_input=", classesJar).
173 FlagWithOutput("--output=", indexCSV)
Colin Crossf1a035e2020-11-16 17:32:30 -0800174 rule.Build("merged-hiddenapi-index", "Merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000175 h.indexCSVPath = indexCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800176}
177
178var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayevb5df8a02020-02-19 16:39:59 +0000179 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
Colin Crossd783bbb2020-07-11 22:30:45 -0700180 unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
Artur Satayevb5df8a02020-02-19 16:39:59 +0000181 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
182 echo "--input-dex=$${INPUT_DEX}";
183 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
184 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
185 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
186 ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800187 CommandDeps: []string{
188 "${config.HiddenAPI}",
189 "${config.SoongZipCmd}",
190 "${config.MergeZipsCmd}",
191 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000192}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800193
Colin Crossf24a22a2019-01-31 14:12:44 -0800194func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800195 uncompressDex bool) {
196
Colin Crossf24a22a2019-01-31 14:12:44 -0800197 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800198
Colin Crosscd964b32019-01-18 22:03:02 -0800199 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
200 // in the input it stays uncompressed in the output.
201 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000202 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000203 tmpOutput := output
204 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800205 if uncompressDex {
206 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000207 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
208 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800209 }
Jiyong Park93e57a02020-02-21 16:04:53 +0900210
211 enforceHiddenApiFlagsToAllMembers := true
David Brazdil91b4e3e2019-01-23 21:04:05 +0000212 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
213 // Disable assertion that all methods/fields have hidden API flags assigned.
214 if !ctx.Config().FrameworksBaseDirExists(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900215 enforceHiddenApiFlagsToAllMembers = false
216 }
217 // b/149353192: when a module is instrumented, jacoco adds synthetic members
218 // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
219 // don't complain when we don't find hidden API flags for the synthetic members.
Paul Duffinc495d2b2020-05-19 21:07:52 +0100220 if j, ok := ctx.Module().(interface {
221 shouldInstrument(android.BaseModuleContext) bool
222 }); ok && j.shouldInstrument(ctx) {
Jiyong Park93e57a02020-02-21 16:04:53 +0900223 enforceHiddenApiFlagsToAllMembers = false
224 }
225
226 if !enforceHiddenApiFlagsToAllMembers {
David Brazdil91b4e3e2019-01-23 21:04:05 +0000227 hiddenapiFlags = "--no-force-assign-all"
228 }
Colin Crosscd964b32019-01-18 22:03:02 -0800229
Colin Cross8faf8fc2019-01-16 15:15:52 -0800230 ctx.Build(pctx, android.BuildParams{
231 Rule: hiddenAPIEncodeDexRule,
232 Description: "hiddenapi encode dex",
233 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000234 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800235 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800236 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800237 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000238 "tmpDir": tmpDir.String(),
239 "soongZipFlags": soongZipFlags,
240 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800241 },
242 })
243
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000244 if uncompressDex {
245 TransformZipAlign(ctx, output, tmpOutput)
246 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800247}