Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 18 | "path/filepath" |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 19 | "sort" |
| 20 | "strings" |
| 21 | "sync" |
| 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
| 28 | var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 29 | Command: "${config.Class2Greylist} --stub-api-flags ${stubAPIFlags} $in $outFlag $out", |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 30 | CommandDeps: []string{"${config.Class2Greylist}"}, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 31 | }, "outFlag", "stubAPIFlags") |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 32 | |
| 33 | func hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) { |
| 34 | flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv") |
| 35 | metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv") |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 36 | stubFlagsCSV := &hiddenAPIPath{ctx.Config().HiddenAPIStubFlags()} |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 37 | |
| 38 | ctx.Build(pctx, android.BuildParams{ |
| 39 | Rule: hiddenAPIGenerateCSVRule, |
| 40 | Description: "hiddenapi flags", |
| 41 | Input: classesJar, |
| 42 | Output: flagsCSV, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 43 | Implicit: stubFlagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 44 | Args: map[string]string{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 45 | "outFlag": "--write-flags-csv", |
| 46 | "stubAPIFlags": stubFlagsCSV.String(), |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 47 | }, |
| 48 | }) |
| 49 | |
| 50 | ctx.Build(pctx, android.BuildParams{ |
| 51 | Rule: hiddenAPIGenerateCSVRule, |
| 52 | Description: "hiddenapi metadata", |
| 53 | Input: classesJar, |
| 54 | Output: metadataCSV, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 55 | Implicit: stubFlagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 56 | Args: map[string]string{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 57 | "outFlag": "--write-metadata-csv", |
| 58 | "stubAPIFlags": stubFlagsCSV.String(), |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 59 | }, |
| 60 | }) |
| 61 | |
| 62 | hiddenAPISaveCSVOutputs(ctx, flagsCSV, metadataCSV) |
| 63 | } |
| 64 | |
| 65 | var 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 Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 71 | `done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && ` + |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 72 | `${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && ` + |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 73 | `${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 Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 79 | }, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags") |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 80 | |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 81 | func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.WritablePath, |
| 82 | uncompressDex bool) { |
| 83 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 84 | flagsCsv := &hiddenAPIPath{ctx.Config().HiddenAPIFlags()} |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 85 | |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 86 | // 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 Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 89 | hiddenapiFlags := "" |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 90 | tmpOutput := output |
| 91 | tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex") |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 92 | if uncompressDex { |
| 93 | soongZipFlags = "-L 0" |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 94 | tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar") |
| 95 | tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned") |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 96 | } |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 97 | // 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 Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 102 | |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 103 | ctx.Build(pctx, android.BuildParams{ |
| 104 | Rule: hiddenAPIEncodeDexRule, |
| 105 | Description: "hiddenapi encode dex", |
| 106 | Input: dexInput, |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 107 | Output: tmpOutput, |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 108 | Implicit: flagsCsv, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 109 | Args: map[string]string{ |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 110 | "flagsCsv": flagsCsv.String(), |
| 111 | "tmpDir": tmpDir.String(), |
| 112 | "soongZipFlags": soongZipFlags, |
| 113 | "hiddenapiFlags": hiddenapiFlags, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 114 | }, |
| 115 | }) |
| 116 | |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 117 | if uncompressDex { |
| 118 | TransformZipAlign(ctx, output, tmpOutput) |
| 119 | } |
| 120 | |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 121 | hiddenAPISaveDexInputs(ctx, dexInput) |
| 122 | } |
| 123 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 124 | var hiddenAPIOutputsKey = android.NewOnceKey("hiddenAPIOutputsKey") |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 125 | |
| 126 | var hiddenAPIOutputsLock sync.Mutex |
| 127 | |
| 128 | func 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 | |
| 136 | func 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 | |
| 146 | func 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 | |
| 155 | func init() { |
| 156 | android.RegisterMakeVarsProvider(pctx, hiddenAPIMakeVars) |
| 157 | } |
| 158 | |
| 159 | func 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 Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 172 | |
| 173 | type hiddenAPIPath struct { |
| 174 | path string |
| 175 | } |
| 176 | |
| 177 | var _ android.Path = (*hiddenAPIPath)(nil) |
| 178 | |
| 179 | func (p *hiddenAPIPath) String() string { return p.path } |
| 180 | func (p *hiddenAPIPath) Ext() string { return filepath.Ext(p.path) } |
| 181 | func (p *hiddenAPIPath) Base() string { return filepath.Base(p.path) } |
| 182 | func (p *hiddenAPIPath) Rel() string { return p.path } |