blob: c7eac733f1cc5b1cabf9f78148ca723db72691c5 [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 Crossfeec25b2019-01-30 17:32:39 -080018 "path/filepath"
Colin Cross8faf8fc2019-01-16 15:15:52 -080019 "sort"
20 "strings"
21 "sync"
22
23 "github.com/google/blueprint"
24
25 "android/soong/android"
26)
27
28var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
David Brazdil0f670a22019-01-18 16:30:03 +000029 Command: "${config.Class2Greylist} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
Colin Cross8faf8fc2019-01-16 15:15:52 -080030 CommandDeps: []string{"${config.Class2Greylist}"},
David Brazdil0f670a22019-01-18 16:30:03 +000031}, "outFlag", "stubAPIFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -080032
33func hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) {
34 flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
35 metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
Colin Crossfeec25b2019-01-30 17:32:39 -080036 stubFlagsCSV := &hiddenAPIPath{ctx.Config().HiddenAPIStubFlags()}
Colin Cross8faf8fc2019-01-16 15:15:52 -080037
38 ctx.Build(pctx, android.BuildParams{
39 Rule: hiddenAPIGenerateCSVRule,
40 Description: "hiddenapi flags",
41 Input: classesJar,
42 Output: flagsCSV,
David Brazdil0f670a22019-01-18 16:30:03 +000043 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -080044 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +000045 "outFlag": "--write-flags-csv",
46 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -080047 },
48 })
49
50 ctx.Build(pctx, android.BuildParams{
51 Rule: hiddenAPIGenerateCSVRule,
52 Description: "hiddenapi metadata",
53 Input: classesJar,
54 Output: metadataCSV,
David Brazdil0f670a22019-01-18 16:30:03 +000055 Implicit: stubFlagsCSV,
Colin Cross8faf8fc2019-01-16 15:15:52 -080056 Args: map[string]string{
David Brazdil0f670a22019-01-18 16:30:03 +000057 "outFlag": "--write-metadata-csv",
58 "stubAPIFlags": stubFlagsCSV.String(),
Colin Cross8faf8fc2019-01-16 15:15:52 -080059 },
60 })
61
62 hiddenAPISaveCSVOutputs(ctx, flagsCSV, metadataCSV)
63}
64
65var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
66 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output && ` +
67 `unzip -o -q $in 'classes*.dex' -d $tmpDir/dex-input && ` +
68 `for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do ` +
69 ` echo "--input-dex=$${INPUT_DEX}"; ` +
70 ` echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})"; ` +
David Brazdil91b4e3e2019-01-23 21:04:05 +000071 `done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && ` +
Colin Crosscd964b32019-01-18 22:03:02 -080072 `${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && ` +
Colin Cross8faf8fc2019-01-16 15:15:52 -080073 `${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" $out $tmpDir/dex.jar $in`,
74 CommandDeps: []string{
75 "${config.HiddenAPI}",
76 "${config.SoongZipCmd}",
77 "${config.MergeZipsCmd}",
78 },
David Brazdil91b4e3e2019-01-23 21:04:05 +000079}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
Colin Cross8faf8fc2019-01-16 15:15:52 -080080
Colin Crosscd964b32019-01-18 22:03:02 -080081func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.WritablePath,
82 uncompressDex bool) {
83
Colin Crossfeec25b2019-01-30 17:32:39 -080084 flagsCsv := &hiddenAPIPath{ctx.Config().HiddenAPIFlags()}
Colin Cross8faf8fc2019-01-16 15:15:52 -080085
Colin Crosscd964b32019-01-18 22:03:02 -080086 // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
87 // in the input it stays uncompressed in the output.
88 soongZipFlags := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +000089 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +000090 tmpOutput := output
91 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -080092 if uncompressDex {
93 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +000094 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
95 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -080096 }
David Brazdil91b4e3e2019-01-23 21:04:05 +000097 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
98 // Disable assertion that all methods/fields have hidden API flags assigned.
99 if !ctx.Config().FrameworksBaseDirExists(ctx) {
100 hiddenapiFlags = "--no-force-assign-all"
101 }
Colin Crosscd964b32019-01-18 22:03:02 -0800102
Colin Cross8faf8fc2019-01-16 15:15:52 -0800103 ctx.Build(pctx, android.BuildParams{
104 Rule: hiddenAPIEncodeDexRule,
105 Description: "hiddenapi encode dex",
106 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000107 Output: tmpOutput,
David Brazdil91b4e3e2019-01-23 21:04:05 +0000108 Implicit: flagsCsv,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800109 Args: map[string]string{
David Brazdil91b4e3e2019-01-23 21:04:05 +0000110 "flagsCsv": flagsCsv.String(),
111 "tmpDir": tmpDir.String(),
112 "soongZipFlags": soongZipFlags,
113 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800114 },
115 })
116
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000117 if uncompressDex {
118 TransformZipAlign(ctx, output, tmpOutput)
119 }
120
Colin Cross8faf8fc2019-01-16 15:15:52 -0800121 hiddenAPISaveDexInputs(ctx, dexInput)
122}
123
Colin Cross571cccf2019-02-04 11:22:08 -0800124var hiddenAPIOutputsKey = android.NewOnceKey("hiddenAPIOutputsKey")
Colin Cross8faf8fc2019-01-16 15:15:52 -0800125
126var hiddenAPIOutputsLock sync.Mutex
127
128func hiddenAPIGetOutputs(config android.Config) (*android.Paths, *android.Paths, *android.Paths) {
129 type threePathsPtrs [3]*android.Paths
130 s := config.Once(hiddenAPIOutputsKey, func() interface{} {
131 return threePathsPtrs{new(android.Paths), new(android.Paths), new(android.Paths)}
132 }).(threePathsPtrs)
133 return s[0], s[1], s[2]
134}
135
136func hiddenAPISaveCSVOutputs(ctx android.ModuleContext, flagsCSV, metadataCSV android.Path) {
137 flagsCSVList, metadataCSVList, _ := hiddenAPIGetOutputs(ctx.Config())
138
139 hiddenAPIOutputsLock.Lock()
140 defer hiddenAPIOutputsLock.Unlock()
141
142 *flagsCSVList = append(*flagsCSVList, flagsCSV)
143 *metadataCSVList = append(*metadataCSVList, metadataCSV)
144}
145
146func hiddenAPISaveDexInputs(ctx android.ModuleContext, dexInput android.Path) {
147 _, _, dexInputList := hiddenAPIGetOutputs(ctx.Config())
148
149 hiddenAPIOutputsLock.Lock()
150 defer hiddenAPIOutputsLock.Unlock()
151
152 *dexInputList = append(*dexInputList, dexInput)
153}
154
155func init() {
156 android.RegisterMakeVarsProvider(pctx, hiddenAPIMakeVars)
157}
158
159func hiddenAPIMakeVars(ctx android.MakeVarsContext) {
160 flagsCSVList, metadataCSVList, dexInputList := hiddenAPIGetOutputs(ctx.Config())
161
162 export := func(name string, paths *android.Paths) {
163 s := paths.Strings()
164 sort.Strings(s)
165 ctx.Strict(name, strings.Join(s, " "))
166 }
167
168 export("SOONG_HIDDENAPI_FLAGS", flagsCSVList)
169 export("SOONG_HIDDENAPI_GREYLIST_METADATA", metadataCSVList)
170 export("SOONG_HIDDENAPI_DEX_INPUTS", dexInputList)
171}
Colin Crossfeec25b2019-01-30 17:32:39 -0800172
173type hiddenAPIPath struct {
174 path string
175}
176
177var _ android.Path = (*hiddenAPIPath)(nil)
178
179func (p *hiddenAPIPath) String() string { return p.path }
180func (p *hiddenAPIPath) Ext() string { return filepath.Ext(p.path) }
181func (p *hiddenAPIPath) Base() string { return filepath.Base(p.path) }
182func (p *hiddenAPIPath) Rel() string { return p.path }