blob: de72e7c7991fd4378fa5bf340c2744513fc5a368 [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 && ` +
71 `${config.SoongZipCmd} -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && ` +
72 `${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 },
78}, "flags", "tmpDir")
79
80func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.WritablePath) {
Colin Cross8faf8fc2019-01-16 15:15:52 -080081 flags := &bootImagePath{ctx.Config().HiddenAPIFlags()}
82
83 ctx.Build(pctx, android.BuildParams{
84 Rule: hiddenAPIEncodeDexRule,
85 Description: "hiddenapi encode dex",
86 Input: dexInput,
87 Output: output,
88 Implicit: flags,
89 Args: map[string]string{
90 "flags": flags.String(),
91 "tmpDir": android.PathForModuleOut(ctx, "hiddenapi", "dex").String(),
92 },
93 })
94
95 hiddenAPISaveDexInputs(ctx, dexInput)
96}
97
98const hiddenAPIOutputsKey = "hiddenAPIOutputsKey"
99
100var hiddenAPIOutputsLock sync.Mutex
101
102func hiddenAPIGetOutputs(config android.Config) (*android.Paths, *android.Paths, *android.Paths) {
103 type threePathsPtrs [3]*android.Paths
104 s := config.Once(hiddenAPIOutputsKey, func() interface{} {
105 return threePathsPtrs{new(android.Paths), new(android.Paths), new(android.Paths)}
106 }).(threePathsPtrs)
107 return s[0], s[1], s[2]
108}
109
110func hiddenAPISaveCSVOutputs(ctx android.ModuleContext, flagsCSV, metadataCSV android.Path) {
111 flagsCSVList, metadataCSVList, _ := hiddenAPIGetOutputs(ctx.Config())
112
113 hiddenAPIOutputsLock.Lock()
114 defer hiddenAPIOutputsLock.Unlock()
115
116 *flagsCSVList = append(*flagsCSVList, flagsCSV)
117 *metadataCSVList = append(*metadataCSVList, metadataCSV)
118}
119
120func hiddenAPISaveDexInputs(ctx android.ModuleContext, dexInput android.Path) {
121 _, _, dexInputList := hiddenAPIGetOutputs(ctx.Config())
122
123 hiddenAPIOutputsLock.Lock()
124 defer hiddenAPIOutputsLock.Unlock()
125
126 *dexInputList = append(*dexInputList, dexInput)
127}
128
129func init() {
130 android.RegisterMakeVarsProvider(pctx, hiddenAPIMakeVars)
131}
132
133func hiddenAPIMakeVars(ctx android.MakeVarsContext) {
134 flagsCSVList, metadataCSVList, dexInputList := hiddenAPIGetOutputs(ctx.Config())
135
136 export := func(name string, paths *android.Paths) {
137 s := paths.Strings()
138 sort.Strings(s)
139 ctx.Strict(name, strings.Join(s, " "))
140 }
141
142 export("SOONG_HIDDENAPI_FLAGS", flagsCSVList)
143 export("SOONG_HIDDENAPI_GREYLIST_METADATA", metadataCSVList)
144 export("SOONG_HIDDENAPI_DEX_INPUTS", dexInputList)
145}