blob: a06a666f28aea59fa3a3c70661e12c935f050497 [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 (
18 "sort"
19 "strings"
20 "sync"
21
22 "github.com/google/blueprint"
23
24 "android/soong/android"
25)
26
27var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
David Brazdil0f670a22019-01-18 16:30:03 +000028 Command: "${config.Class2Greylist} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
Colin Cross8faf8fc2019-01-16 15:15:52 -080029 CommandDeps: []string{"${config.Class2Greylist}"},
David Brazdil0f670a22019-01-18 16:30:03 +000030}, "outFlag", "stubAPIFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -080031
32func hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) {
33 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
34 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
David Brazdil0f670a22019-01-18 16:30:03 +000035 stubFlagsCSV := &bootImagePath{ctx.Config().HiddenAPIStubFlags()}
Colin Cross8faf8fc2019-01-16 15:15:52 -080036
37 ctx.Build(pctx, android.BuildParams{
38 Rule: hiddenAPIGenerateCSVRule,
39 Description: "hiddenapi flags",
40 Input: classesJar,
41 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +000042 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -080043 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +000044 "outFlag": "--write-flags-csv",
45 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -080046 },
47 })
48
49 ctx.Build(pctx, android.BuildParams{
50 Rule: hiddenAPIGenerateCSVRule,
51 Description: "hiddenapi metadata",
52 Input: classesJar,
53 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +000054 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -080055 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +000056 "outFlag": "--write-metadata-csv",
57 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -080058 },
59 })
60
61 hiddenAPISaveCSVOutputs(ctx, flagsCSV, metadataCSV)
62}
63
64var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
65 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output && ` +
66 `unzip -o -q $in 'classes*.dex' -d $tmpDir/dex-input && ` +
67 `for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do ` +
68 ` echo "--input-dex=$${INPUT_DEX}"; ` +
69 ` echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})"; ` +
70 `done | xargs ${config.HiddenAPI} encode --api-flags=$flags && ` +
Colin Crosscd964b32019-01-18 22:03:02 -080071 `${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && ` +
Colin Cross8faf8fc2019-01-16 15:15:52 -080072 `${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" $out $tmpDir/dex.jar $in`,
73 CommandDeps: []string{
74 "${config.HiddenAPI}",
75 "${config.SoongZipCmd}",
76 "${config.MergeZipsCmd}",
77 },
Colin Crosscd964b32019-01-18 22:03:02 -080078}, "flags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -080079
Colin Crosscd964b32019-01-18 22:03:02 -080080func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.WritablePath,
81 uncompressDex bool) {
82
Colin Cross8faf8fc2019-01-16 15:15:52 -080083 flags := &bootImagePath{ctx.Config().HiddenAPIFlags()}
84
Colin Crosscd964b32019-01-18 22:03:02 -080085 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
86 // in the input it stays uncompressed in the output.
87 soongZipFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +000088 tmpOutput := output
89 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -080090 if uncompressDex {
91 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +000092 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
93 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -080094 }
95
Colin Cross8faf8fc2019-01-16 15:15:52 -080096 ctx.Build(pctx, android.BuildParams{
97 Rule: hiddenAPIEncodeDexRule,
98 Description: "hiddenapi encode dex",
99 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000100 Output: tmpOutput,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800101 Implicit: flags,
102 Args: map[string]string{
Colin Crosscd964b32019-01-18 22:03:02 -0800103 "flags": flags.String(),
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000104 "tmpDir": tmpDir.String(),
Colin Crosscd964b32019-01-18 22:03:02 -0800105 "soongZipFlags": soongZipFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800106 },
107 })
108
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000109 if uncompressDex {
110 TransformZipAlign(ctx, output, tmpOutput)
111 }
112
Colin Cross8faf8fc2019-01-16 15:15:52 -0800113 hiddenAPISaveDexInputs(ctx, dexInput)
114}
115
116const hiddenAPIOutputsKey = "hiddenAPIOutputsKey"
117
118var hiddenAPIOutputsLock sync.Mutex
119
120func hiddenAPIGetOutputs(config android.Config) (*android.Paths, *android.Paths, *android.Paths) {
121 type threePathsPtrs [3]*android.Paths
122 s := config.Once(hiddenAPIOutputsKey, func() interface{} {
123 return threePathsPtrs{new(android.Paths), new(android.Paths), new(android.Paths)}
124 }).(threePathsPtrs)
125 return s[0], s[1], s[2]
126}
127
128func hiddenAPISaveCSVOutputs(ctx android.ModuleContext, flagsCSV, metadataCSV android.Path) {
129 flagsCSVList, metadataCSVList, _ := hiddenAPIGetOutputs(ctx.Config())
130
131 hiddenAPIOutputsLock.Lock()
132 defer hiddenAPIOutputsLock.Unlock()
133
134 *flagsCSVList = append(*flagsCSVList, flagsCSV)
135 *metadataCSVList = append(*metadataCSVList, metadataCSV)
136}
137
138func hiddenAPISaveDexInputs(ctx android.ModuleContext, dexInput android.Path) {
139 _, _, dexInputList := hiddenAPIGetOutputs(ctx.Config())
140
141 hiddenAPIOutputsLock.Lock()
142 defer hiddenAPIOutputsLock.Unlock()
143
144 *dexInputList = append(*dexInputList, dexInput)
145}
146
147func init() {
148 android.RegisterMakeVarsProvider(pctx, hiddenAPIMakeVars)
149}
150
151func hiddenAPIMakeVars(ctx android.MakeVarsContext) {
152 flagsCSVList, metadataCSVList, dexInputList := hiddenAPIGetOutputs(ctx.Config())
153
154 export := func(name string, paths *android.Paths) {
155 s := paths.Strings()
156 sort.Strings(s)
157 ctx.Strict(name, strings.Join(s, " "))
158 }
159
160 export("SOONG_HIDDENAPI_FLAGS", flagsCSVList)
161 export("SOONG_HIDDENAPI_GREYLIST_METADATA", metadataCSVList)
162 export("SOONG_HIDDENAPI_DEX_INPUTS", dexInputList)
163}