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