blob: 104cd767b8100845145187feb61fc19d6afbe000 [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 Duffine5b56572019-02-15 14:27:22 +000060 // Check to see if this module provides part of the hiddenapi, i.e. is a boot jar or a white listed
61 // library.
62 isProvidingJar := isBootJar || inList(ctx.ModuleName(), config.HiddenAPIProvidingNonBootJars)
63
64 // If this module provides part of the hiddenapi or is a special module that simply provides information
65 // about the hiddenapi then extract information about the hiddenapi from the UnsupportedAppUsage
66 // annotations compiled into the classes.jar.
67 if isProvidingJar || inList(ctx.ModuleName(), config.HiddenAPIExtraAppUsageJars) {
Colin Crossf24a22a2019-01-31 14:12:44 -080068 // Derive the greylist from classes jar.
69 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
70 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
71 hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, implementationJar)
72 h.flagsCSVPath = flagsCSV
73 h.metadataCSVPath = metadataCSV
74 }
Paul Duffine5b56572019-02-15 14:27:22 +000075
76 // If this module provides part of the hiddenapi then encode the information about the hiddenapi into
77 // the dex file created for this module.
78 if isProvidingJar {
Colin Crossf24a22a2019-01-31 14:12:44 -080079 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", ctx.ModuleName()+".jar")
80 h.bootDexJarPath = dexJar
81 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
82 dexJar = hiddenAPIJar
83 }
84 }
85
86 return dexJar
87}
88
89func hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV android.WritablePath,
90 classesJar android.Path) {
91
92 stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
Colin Cross8faf8fc2019-01-16 15:15:52 -080093
94 ctx.Build(pctx, android.BuildParams{
95 Rule: hiddenAPIGenerateCSVRule,
96 Description: "hiddenapi flags",
97 Input: classesJar,
98 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +000099 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800100 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +0000101 "outFlag": "--write-flags-csv",
102 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800103 },
104 })
105
106 ctx.Build(pctx, android.BuildParams{
107 Rule: hiddenAPIGenerateCSVRule,
108 Description: "hiddenapi metadata",
109 Input: classesJar,
110 Output: metadataCSV,
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-metadata-csv",
114 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -0800115 },
116 })
117
Colin Cross8faf8fc2019-01-16 15:15:52 -0800118}
119
120var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
121 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output && ` +
122 `unzip -o -q $in 'classes*.dex' -d $tmpDir/dex-input && ` +
123 `for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do ` +
124 ` echo "--input-dex=$${INPUT_DEX}"; ` +
125 ` echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})"; ` +
David Brazdil91b4e3e2019-01-23 21:04:05 +0000126 `done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && ` +
Colin Crosscd964b32019-01-18 22:03:02 -0800127 `${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && ` +
Colin Cross8faf8fc2019-01-16 15:15:52 -0800128 `${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" $out $tmpDir/dex.jar $in`,
129 CommandDeps: []string{
130 "${config.HiddenAPI}",
131 "${config.SoongZipCmd}",
132 "${config.MergeZipsCmd}",
133 },
David Brazdil91b4e3e2019-01-23 21:04:05 +0000134}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800135
Colin Crossf24a22a2019-01-31 14:12:44 -0800136func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
Colin Crosscd964b32019-01-18 22:03:02 -0800137 uncompressDex bool) {
138
Colin Crossf24a22a2019-01-31 14:12:44 -0800139 flagsCSV := hiddenAPISingletonPaths(ctx).flags
Colin Cross8faf8fc2019-01-16 15:15:52 -0800140
Colin Crosscd964b32019-01-18 22:03:02 -0800141 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
142 // in the input it stays uncompressed in the output.
143 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +0000144 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000145 tmpOutput := output
146 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -0800147 if uncompressDex {
148 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000149 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
150 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -0800151 }
David Brazdil91b4e3e2019-01-23 21:04:05 +0000152 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
153 // Disable assertion that all methods/fields have hidden API flags assigned.
154 if !ctx.Config().FrameworksBaseDirExists(ctx) {
155 hiddenapiFlags = "--no-force-assign-all"
156 }
Colin Crosscd964b32019-01-18 22:03:02 -0800157
Colin Cross8faf8fc2019-01-16 15:15:52 -0800158 ctx.Build(pctx, android.BuildParams{
159 Rule: hiddenAPIEncodeDexRule,
160 Description: "hiddenapi encode dex",
161 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000162 Output: tmpOutput,
Colin Crossf24a22a2019-01-31 14:12:44 -0800163 Implicit: flagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800164 Args: map[string]string{
Colin Crossf24a22a2019-01-31 14:12:44 -0800165 "flagsCsv": flagsCSV.String(),
David Brazdil91b4e3e2019-01-23 21:04:05 +0000166 "tmpDir": tmpDir.String(),
167 "soongZipFlags": soongZipFlags,
168 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800169 },
170 })
171
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000172 if uncompressDex {
173 TransformZipAlign(ctx, output, tmpOutput)
174 }
Colin Cross8faf8fc2019-01-16 15:15:52 -0800175}