blob: 67df57552f2fca42c8490a159e1c31824f443faa [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})"; ` +
David Brazdil91b4e3e2019-01-23 21:04:05 +000070 `done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && ` +
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 },
David Brazdil91b4e3e2019-01-23 21:04:05 +000078}, "flagsCsv", "hiddenapiFlags", "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
David Brazdil91b4e3e2019-01-23 21:04:05 +000083 flagsCsv := &bootImagePath{ctx.Config().HiddenAPIFlags()}
Colin Cross8faf8fc2019-01-16 15:15:52 -080084
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 := ""
David Brazdil91b4e3e2019-01-23 21:04:05 +000088 hiddenapiFlags := ""
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +000089 tmpOutput := output
90 tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
Colin Crosscd964b32019-01-18 22:03:02 -080091 if uncompressDex {
92 soongZipFlags = "-L 0"
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +000093 tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
94 tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
Colin Crosscd964b32019-01-18 22:03:02 -080095 }
David Brazdil91b4e3e2019-01-23 21:04:05 +000096 // If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
97 // Disable assertion that all methods/fields have hidden API flags assigned.
98 if !ctx.Config().FrameworksBaseDirExists(ctx) {
99 hiddenapiFlags = "--no-force-assign-all"
100 }
Colin Crosscd964b32019-01-18 22:03:02 -0800101
Colin Cross8faf8fc2019-01-16 15:15:52 -0800102 ctx.Build(pctx, android.BuildParams{
103 Rule: hiddenAPIEncodeDexRule,
104 Description: "hiddenapi encode dex",
105 Input: dexInput,
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000106 Output: tmpOutput,
David Brazdil91b4e3e2019-01-23 21:04:05 +0000107 Implicit: flagsCsv,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800108 Args: map[string]string{
David Brazdil91b4e3e2019-01-23 21:04:05 +0000109 "flagsCsv": flagsCsv.String(),
110 "tmpDir": tmpDir.String(),
111 "soongZipFlags": soongZipFlags,
112 "hiddenapiFlags": hiddenapiFlags,
Colin Cross8faf8fc2019-01-16 15:15:52 -0800113 },
114 })
115
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000116 if uncompressDex {
117 TransformZipAlign(ctx, output, tmpOutput)
118 }
119
Colin Cross8faf8fc2019-01-16 15:15:52 -0800120 hiddenAPISaveDexInputs(ctx, dexInput)
121}
122
123const hiddenAPIOutputsKey = "hiddenAPIOutputsKey"
124
125var hiddenAPIOutputsLock sync.Mutex
126
127func hiddenAPIGetOutputs(config android.Config) (*android.Paths, *android.Paths, *android.Paths) {
128 type threePathsPtrs [3]*android.Paths
129 s := config.Once(hiddenAPIOutputsKey, func() interface{} {
130 return threePathsPtrs{new(android.Paths), new(android.Paths), new(android.Paths)}
131 }).(threePathsPtrs)
132 return s[0], s[1], s[2]
133}
134
135func hiddenAPISaveCSVOutputs(ctx android.ModuleContext, flagsCSV, metadataCSV android.Path) {
136 flagsCSVList, metadataCSVList, _ := hiddenAPIGetOutputs(ctx.Config())
137
138 hiddenAPIOutputsLock.Lock()
139 defer hiddenAPIOutputsLock.Unlock()
140
141 *flagsCSVList = append(*flagsCSVList, flagsCSV)
142 *metadataCSVList = append(*metadataCSVList, metadataCSV)
143}
144
145func hiddenAPISaveDexInputs(ctx android.ModuleContext, dexInput android.Path) {
146 _, _, dexInputList := hiddenAPIGetOutputs(ctx.Config())
147
148 hiddenAPIOutputsLock.Lock()
149 defer hiddenAPIOutputsLock.Unlock()
150
151 *dexInputList = append(*dexInputList, dexInput)
152}
153
154func init() {
155 android.RegisterMakeVarsProvider(pctx, hiddenAPIMakeVars)
156}
157
158func hiddenAPIMakeVars(ctx android.MakeVarsContext) {
159 flagsCSVList, metadataCSVList, dexInputList := hiddenAPIGetOutputs(ctx.Config())
160
161 export := func(name string, paths *android.Paths) {
162 s := paths.Strings()
163 sort.Strings(s)
164 ctx.Strict(name, strings.Join(s, " "))
165 }
166
167 export("SOONG_HIDDENAPI_FLAGS", flagsCSVList)
168 export("SOONG_HIDDENAPI_GREYLIST_METADATA", metadataCSVList)
169 export("SOONG_HIDDENAPI_DEX_INPUTS", dexInputList)
170}