blob: d48c7675d83d4f45e56a370eaf3c03bdaf94fdb2 [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{
David Brazdil0f670a22019-01-18 16:30:03 +000026 Command: "${config.Class2Greylist} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
Colin Cross8faf8fc2019-01-16 15:15:52 -080027 CommandDeps: []string{"${config.Class2Greylist}"},
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 {
Colin Crossf24a22a2019-01-31 14:12:44 -080031 bootDexJarPath android.Path
Artur Satayevb5df8a02020-02-19 16:39:59 +000032 flagsCSVPath android.Path
33 indexCSVPath android.Path
34 metadataCSVPath android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080035}
36
37func (h *hiddenAPI) flagsCSV() android.Path {
38 return h.flagsCSVPath
39}
40
41func (h *hiddenAPI) metadataCSV() android.Path {
42 return h.metadataCSVPath
43}
44
45func (h *hiddenAPI) bootDexJar() android.Path {
46 return h.bootDexJarPath
47}
48
Artur Satayevb5df8a02020-02-19 16:39:59 +000049func (h *hiddenAPI) indexCSV() android.Path {
50 return h.indexCSVPath
51}
52
Colin Crossf24a22a2019-01-31 14:12:44 -080053type hiddenAPIIntf interface {
Colin Crossf24a22a2019-01-31 14:12:44 -080054 bootDexJar() android.Path
Artur Satayevb5df8a02020-02-19 16:39:59 +000055 flagsCSV() android.Path
56 indexCSV() android.Path
57 metadataCSV() android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -080058}
59
60var _ hiddenAPIIntf = (*hiddenAPI)(nil)
61
Artur Satayevb5df8a02020-02-19 16:39:59 +000062func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, dexJar android.ModuleOutPath,
63 implementationJar android.Path, uncompressDex bool) android.ModuleOutPath {
Colin Crossf24a22a2019-01-31 14:12:44 -080064 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
Paul Duffind2aceca2019-02-28 16:13:20 +000065 name := ctx.ModuleName()
66
67 // Modules whose names are of the format <x>-hiddenapi provide hiddenapi information
68 // for the boot jar module <x>. Otherwise, the module provides information for itself.
69 // Either way extract the name of the boot jar module.
70 bootJarName := strings.TrimSuffix(name, "-hiddenapi")
71
72 // If this module is on the boot jars list (or providing information for a module
73 // on the list) then extract the hiddenapi information from it, and if necessary
74 // encode that information in the generated dex file.
75 //
76 // It is important that hiddenapi information is only gathered for/from modules on
77 // that are actually on the boot jars list because the runtime only enforces access
78 // to the hidden API for the bootclassloader. If information is gathered for modules
79 // not on the list then that will cause failures in the CtsHiddenApiBlacklist...
80 // tests.
81 if inList(bootJarName, ctx.Config().BootJars()) {
Colin Crossf24a22a2019-01-31 14:12:44 -080082 // Derive the greylist from classes jar.
83 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
84 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
Artur Satayevb5df8a02020-02-19 16:39:59 +000085 indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
86 h.hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, indexCSV, implementationJar)
Paul Duffind2aceca2019-02-28 16:13:20 +000087
88 // If this module is actually on the boot jars list and not providing
89 // hiddenapi information for a module on the boot jars list then encode
90 // the gathered information in the generated dex file.
91 if name == bootJarName {
92 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar")
93 h.bootDexJarPath = dexJar
94 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
95 dexJar = hiddenAPIJar
96 }
Colin Crossf24a22a2019-01-31 14:12:44 -080097 }
98 }
99
100 return dexJar
101}
102
Artur Satayevb5df8a02020-02-19 16:39:59 +0000103func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV, indexCSV android.WritablePath, classesJar android.Path) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800104 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800105
106 ctx.Build(pctx, android.BuildParams{
107 Rule: hiddenAPIGenerateCSVRule,
108 Description: "hiddenapi flags",
109 Input: classesJar,
110 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000111 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800112 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000113 "outFlag": "--write-flags-csv",
114 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800115 },
116 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000117 h.flagsCSVPath = flagsCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800118
119 ctx.Build(pctx, android.BuildParams{
120 Rule: hiddenAPIGenerateCSVRule,
121 Description: "hiddenapi metadata",
122 Input: classesJar,
123 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000124 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800125 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000126 "outFlag": "--write-metadata-csv",
127 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800128 },
129 })
Artur Satayevb5df8a02020-02-19 16:39:59 +0000130 h.metadataCSVPath = metadataCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800131
Artur Satayevb5df8a02020-02-19 16:39:59 +0000132 rule := android.NewRuleBuilder()
133 rule.Command().
134 BuiltTool(ctx, "merge_csv").
135 FlagWithInput("--zip_input=", classesJar).
136 FlagWithOutput("--output=", indexCSV)
137 rule.Build(pctx, ctx, "merged-hiddenapi-index", "Merged Hidden API index")
138 h.indexCSVPath = indexCSV
Colin Cross8faf8fc2019-01-16 15:15:52 -0800139}
140
141var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
Artur Satayevb5df8a02020-02-19 16:39:59 +0000142 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
143 unzip -o -q $in 'classes*.dex' -d $tmpDir/dex-input &&
144 for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
145 echo "--input-dex=$${INPUT_DEX}";
146 echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
147 done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
148 ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
149 ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800150 CommandDeps: []string{
151 "${config.HiddenAPI}",
152 "${config.SoongZipCmd}",
153 "${config.MergeZipsCmd}",
154 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000155}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800156
Colin Crossf24a22a2019-01-31 14:12:44 -0800157func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800158 uncompressDex bool) {
159
Colin Crossf24a22a2019-01-31 14:12:44 -0800160 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800161
Colin Crosscd964b32019-01-18 22:03:02 -0800162 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
163 // in the input it stays uncompressed in the output.
164 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000165 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000166 tmpOutput := output
167 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800168 if uncompressDex {
169 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000170 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
171 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800172 }
David Brazdil91b4e3e2019-01-23 21:04:05 +0000173 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
174 // Disable assertion that all methods/fields have hidden API flags assigned.
175 if !ctx.Config().FrameworksBaseDirExists(ctx) {
176 hiddenapiFlags = "--no-force-assign-all"
177 }
Colin Crosscd964b32019-01-18 22:03:02 -0800178
Colin Cross8faf8fc2019-01-16 15:15:52 -0800179 ctx.Build(pctx, android.BuildParams{
180 Rule: hiddenAPIEncodeDexRule,
181 Description: "hiddenapi encode dex",
182 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000183 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800184 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800185 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800186 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000187 "tmpDir": tmpDir.String(),
188 "soongZipFlags": soongZipFlags,
189 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800190 },
191 })
192
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000193 if uncompressDex {
194 TransformZipAlign(ctx, output, tmpOutput)
195 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800196}