blob: 51ed3dda7eb07a1e6c6a9b14a4cb68e4925eb5c0 [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"
Colin Crossf24a22a2019-01-31 14:12:44 -080021 "android/soong/java/config"
Colin Cross8faf8fc2019-01-16 15:15:52 -080022)
23
24var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
David Brazdil0f670a22019-01-18 16:30:03 +000025 Command: "${config.Class2Greylist} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
Colin Cross8faf8fc2019-01-16 15:15:52 -080026 CommandDeps: []string{"${config.Class2Greylist}"},
David Brazdil0f670a22019-01-18 16:30:03 +000027}, "outFlag", "stubAPIFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -080028
Colin Crossf24a22a2019-01-31 14:12:44 -080029type hiddenAPI struct {
30 flagsCSVPath android.Path
31 metadataCSVPath android.Path
32 bootDexJarPath android.Path
33}
34
35func (h *hiddenAPI) flagsCSV() android.Path {
36 return h.flagsCSVPath
37}
38
39func (h *hiddenAPI) metadataCSV() android.Path {
40 return h.metadataCSVPath
41}
42
43func (h *hiddenAPI) bootDexJar() android.Path {
44 return h.bootDexJarPath
45}
46
47type hiddenAPIIntf interface {
48 flagsCSV() android.Path
49 metadataCSV() android.Path
50 bootDexJar() android.Path
51}
52
53var _ hiddenAPIIntf = (*hiddenAPI)(nil)
54
55func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, dexJar android.ModuleOutPath, implementationJar android.Path,
56 uncompressDex bool) android.ModuleOutPath {
57
58 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
59 isBootJar := inList(ctx.ModuleName(), ctx.Config().BootJars())
Paul Duffinc02e8342019-02-27 12:37:45 +000060 if isBootJar || inList(ctx.ModuleName(), config.HiddenAPIExtraAppUsageJars) {
Colin Crossf24a22a2019-01-31 14:12:44 -080061 // Derive the greylist from classes jar.
62 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
63 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
64 hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, implementationJar)
65 h.flagsCSVPath = flagsCSV
66 h.metadataCSVPath = metadataCSV
67 }
Paul Duffinc02e8342019-02-27 12:37:45 +000068 if isBootJar {
Colin Crossf24a22a2019-01-31 14:12:44 -080069 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", ctx.ModuleName()+".jar")
70 h.bootDexJarPath = dexJar
71 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
72 dexJar = hiddenAPIJar
73 }
74 }
75
76 return dexJar
77}
78
79func hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV android.WritablePath,
80 classesJar android.Path) {
81
82 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -080083
84 ctx.Build(pctx, android.BuildParams{
85 Rule: hiddenAPIGenerateCSVRule,
86 Description: "hiddenapi flags",
87 Input: classesJar,
88 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +000089 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -080090 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +000091 "outFlag": "--write-flags-csv",
92 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -080093 },
94 })
95
96 ctx.Build(pctx, android.BuildParams{
97 Rule: hiddenAPIGenerateCSVRule,
98 Description: "hiddenapi metadata",
99 Input: classesJar,
100 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +0000101 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800102 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000103 "outFlag": "--write-metadata-csv",
104 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800105 },
106 })
107
Colin Cross8faf8fc2019-01-16 15:15:52 -0800108}
109
110var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
111 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output && ` +
112 `unzip -o -q $in 'classes*.dex' -d $tmpDir/dex-input && ` +
113 `for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do ` +
114 ` echo "--input-dex=$${INPUT_DEX}"; ` +
115 ` echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})"; ` +
David Brazdil91b4e3e2019-01-23 21:04:05 +0000116 `done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && ` +
Colin Crosscd964b32019-01-18 22:03:02 -0800117 `${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && ` +
Colin Cross8faf8fc2019-01-16 15:15:52 -0800118 `${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" $out $tmpDir/dex.jar $in`,
119 CommandDeps: []string{
120 "${config.HiddenAPI}",
121 "${config.SoongZipCmd}",
122 "${config.MergeZipsCmd}",
123 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000124}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800125
Colin Crossf24a22a2019-01-31 14:12:44 -0800126func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800127 uncompressDex bool) {
128
Colin Crossf24a22a2019-01-31 14:12:44 -0800129 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800130
Colin Crosscd964b32019-01-18 22:03:02 -0800131 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
132 // in the input it stays uncompressed in the output.
133 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000134 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000135 tmpOutput := output
136 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800137 if uncompressDex {
138 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000139 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
140 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800141 }
David Brazdil91b4e3e2019-01-23 21:04:05 +0000142 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
143 // Disable assertion that all methods/fields have hidden API flags assigned.
144 if !ctx.Config().FrameworksBaseDirExists(ctx) {
145 hiddenapiFlags = "--no-force-assign-all"
146 }
Colin Crosscd964b32019-01-18 22:03:02 -0800147
Colin Cross8faf8fc2019-01-16 15:15:52 -0800148 ctx.Build(pctx, android.BuildParams{
149 Rule: hiddenAPIEncodeDexRule,
150 Description: "hiddenapi encode dex",
151 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000152 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800153 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800154 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800155 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000156 "tmpDir": tmpDir.String(),
157 "soongZipFlags": soongZipFlags,
158 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800159 },
160 })
161
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000162 if uncompressDex {
163 TransformZipAlign(ctx, output, tmpOutput)
164 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800165}