blob: 29f23b1a8227da00661a7c65e14b70075c3170d2 [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{
28 Command: "${config.Class2Greylist} --public-api-list ${publicAPIList} $in $outFlag $out",
29 CommandDeps: []string{"${config.Class2Greylist}"},
30}, "outFlag", "publicAPIList")
31
32func hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) {
33 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
34 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
35 publicList := &bootImagePath{ctx.Config().HiddenAPIPublicList()}
36
37 ctx.Build(pctx, android.BuildParams{
38 Rule: hiddenAPIGenerateCSVRule,
39 Description: "hiddenapi flags",
40 Input: classesJar,
41 Output: flagsCSV,
42 Implicit: publicList,
43 Args: map[string]string{
44 "outFlag": "--write-flags-csv",
45 "publicAPIList": publicList.String(),
46 },
47 })
48
49 ctx.Build(pctx, android.BuildParams{
50 Rule: hiddenAPIGenerateCSVRule,
51 Description: "hiddenapi metadata",
52 Input: classesJar,
53 Output: metadataCSV,
54 Implicit: publicList,
55 Args: map[string]string{
56 "outFlag": "--write-metadata-csv",
57 "publicAPIList": publicList.String(),
58 },
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 := ""
88 if uncompressDex {
89 soongZipFlags = "-L 0"
90 }
91
Colin Cross8faf8fc2019-01-16 15:15:52 -080092 ctx.Build(pctx, android.BuildParams{
93 Rule: hiddenAPIEncodeDexRule,
94 Description: "hiddenapi encode dex",
95 Input: dexInput,
96 Output: output,
97 Implicit: flags,
98 Args: map[string]string{
Colin Crosscd964b32019-01-18 22:03:02 -080099 "flags": flags.String(),
100 "tmpDir": android.PathForModuleOut(ctx, "hiddenapi", "dex").String(),
101 "soongZipFlags": soongZipFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800102 },
103 })
104
105 hiddenAPISaveDexInputs(ctx, dexInput)
106}
107
108const hiddenAPIOutputsKey = "hiddenAPIOutputsKey"
109
110var hiddenAPIOutputsLock sync.Mutex
111
112func hiddenAPIGetOutputs(config android.Config) (*android.Paths, *android.Paths, *android.Paths) {
113 type threePathsPtrs [3]*android.Paths
114 s := config.Once(hiddenAPIOutputsKey, func() interface{} {
115 return threePathsPtrs{new(android.Paths), new(android.Paths), new(android.Paths)}
116 }).(threePathsPtrs)
117 return s[0], s[1], s[2]
118}
119
120func hiddenAPISaveCSVOutputs(ctx android.ModuleContext, flagsCSV, metadataCSV android.Path) {
121 flagsCSVList, metadataCSVList, _ := hiddenAPIGetOutputs(ctx.Config())
122
123 hiddenAPIOutputsLock.Lock()
124 defer hiddenAPIOutputsLock.Unlock()
125
126 *flagsCSVList = append(*flagsCSVList, flagsCSV)
127 *metadataCSVList = append(*metadataCSVList, metadataCSV)
128}
129
130func hiddenAPISaveDexInputs(ctx android.ModuleContext, dexInput android.Path) {
131 _, _, dexInputList := hiddenAPIGetOutputs(ctx.Config())
132
133 hiddenAPIOutputsLock.Lock()
134 defer hiddenAPIOutputsLock.Unlock()
135
136 *dexInputList = append(*dexInputList, dexInput)
137}
138
139func init() {
140 android.RegisterMakeVarsProvider(pctx, hiddenAPIMakeVars)
141}
142
143func hiddenAPIMakeVars(ctx android.MakeVarsContext) {
144 flagsCSVList, metadataCSVList, dexInputList := hiddenAPIGetOutputs(ctx.Config())
145
146 export := func(name string, paths *android.Paths) {
147 s := paths.Strings()
148 sort.Strings(s)
149 ctx.Strict(name, strings.Join(s, " "))
150 }
151
152 export("SOONG_HIDDENAPI_FLAGS", flagsCSVList)
153 export("SOONG_HIDDENAPI_GREYLIST_METADATA", metadataCSVList)
154 export("SOONG_HIDDENAPI_DEX_INPUTS", dexInputList)
155}