blob: f1990511617f93919a2aba80afec268f069adb8f [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 Crossfeec25b2019-01-30 17:32:39 -080018 "path/filepath"
Colin Cross8faf8fc2019-01-16 15:15:52 -080019
20 "github.com/google/blueprint"
21
22 "android/soong/android"
Colin Crossf24a22a2019-01-31 14:12:44 -080023 "android/soong/java/config"
Colin Cross8faf8fc2019-01-16 15:15:52 -080024)
25
26var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
David Brazdil0f670a22019-01-18 16:30:03 +000027 Command: "${config.Class2Greylist} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
Colin Cross8faf8fc2019-01-16 15:15:52 -080028 CommandDeps: []string{"${config.Class2Greylist}"},
David Brazdil0f670a22019-01-18 16:30:03 +000029}, "outFlag", "stubAPIFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -080030
Colin Crossf24a22a2019-01-31 14:12:44 -080031type hiddenAPI struct {
32 flagsCSVPath android.Path
33 metadataCSVPath android.Path
34 bootDexJarPath android.Path
35}
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
49type hiddenAPIIntf interface {
50 flagsCSV() android.Path
51 metadataCSV() android.Path
52 bootDexJar() android.Path
53}
54
55var _ hiddenAPIIntf = (*hiddenAPI)(nil)
56
57func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, dexJar android.ModuleOutPath, implementationJar android.Path,
58 uncompressDex bool) android.ModuleOutPath {
59
60 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
61 isBootJar := inList(ctx.ModuleName(), ctx.Config().BootJars())
62 if isBootJar || inList(ctx.ModuleName(), config.HiddenAPIExtraAppUsageJars) {
63 // Derive the greylist from classes jar.
64 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
65 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
66 hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, implementationJar)
67 h.flagsCSVPath = flagsCSV
68 h.metadataCSVPath = metadataCSV
69 }
70 if isBootJar {
71 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", ctx.ModuleName()+".jar")
72 h.bootDexJarPath = dexJar
73 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
74 dexJar = hiddenAPIJar
75 }
76 }
77
78 return dexJar
79}
80
81func hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV android.WritablePath,
82 classesJar android.Path) {
83
84 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -080085
86 ctx.Build(pctx, android.BuildParams{
87 Rule: hiddenAPIGenerateCSVRule,
88 Description: "hiddenapi flags",
89 Input: classesJar,
90 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +000091 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -080092 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +000093 "outFlag": "--write-flags-csv",
94 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -080095 },
96 })
97
98 ctx.Build(pctx, android.BuildParams{
99 Rule: hiddenAPIGenerateCSVRule,
100 Description: "hiddenapi metadata",
101 Input: classesJar,
102 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000103 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800104 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000105 "outFlag": "--write-metadata-csv",
106 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800107 },
108 })
109
Colin Cross8faf8fc2019-01-16 15:15:52 -0800110}
111
112var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
113 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output && ` +
114 `unzip -o -q $in 'classes*.dex' -d $tmpDir/dex-input && ` +
115 `for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do ` +
116 ` echo "--input-dex=$${INPUT_DEX}"; ` +
117 ` echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})"; ` +
David Brazdil91b4e3e2019-01-23 21:04:05 +0000118 `done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && ` +
Colin Crosscd964b32019-01-18 22:03:02 -0800119 `${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && ` +
Colin Cross8faf8fc2019-01-16 15:15:52 -0800120 `${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" $out $tmpDir/dex.jar $in`,
121 CommandDeps: []string{
122 "${config.HiddenAPI}",
123 "${config.SoongZipCmd}",
124 "${config.MergeZipsCmd}",
125 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000126}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800127
Colin Crossf24a22a2019-01-31 14:12:44 -0800128func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800129 uncompressDex bool) {
130
Colin Crossf24a22a2019-01-31 14:12:44 -0800131 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800132
Colin Crosscd964b32019-01-18 22:03:02 -0800133 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
134 // in the input it stays uncompressed in the output.
135 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000136 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000137 tmpOutput := output
138 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800139 if uncompressDex {
140 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000141 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
142 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800143 }
David Brazdil91b4e3e2019-01-23 21:04:05 +0000144 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
145 // Disable assertion that all methods/fields have hidden API flags assigned.
146 if !ctx.Config().FrameworksBaseDirExists(ctx) {
147 hiddenapiFlags = "--no-force-assign-all"
148 }
Colin Crosscd964b32019-01-18 22:03:02 -0800149
Colin Cross8faf8fc2019-01-16 15:15:52 -0800150 ctx.Build(pctx, android.BuildParams{
151 Rule: hiddenAPIEncodeDexRule,
152 Description: "hiddenapi encode dex",
153 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000154 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800155 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800156 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800157 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000158 "tmpDir": tmpDir.String(),
159 "soongZipFlags": soongZipFlags,
160 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800161 },
162 })
163
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000164 if uncompressDex {
165 TransformZipAlign(ctx, output, tmpOutput)
166 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800167}
Colin Crossfeec25b2019-01-30 17:32:39 -0800168
169type hiddenAPIPath struct {
170 path string
171}
172
173var _ android.Path = (*hiddenAPIPath)(nil)
174
175func (p *hiddenAPIPath) String() string { return p.path }
176func (p *hiddenAPIPath) Ext() string { return filepath.Ext(p.path) }
177func (p *hiddenAPIPath) Base() string { return filepath.Base(p.path) }
178func (p *hiddenAPIPath) Rel() string { return p.path }