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 ( |
| 18 | "sort" |
| 19 | "strings" |
| 20 | "sync" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame^] | 28 | Command: "${config.Class2Greylist} --stub-api-flags ${stubAPIFlags} $in $outFlag $out", |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 29 | CommandDeps: []string{"${config.Class2Greylist}"}, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame^] | 30 | }, "outFlag", "stubAPIFlags") |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 31 | |
| 32 | func hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) { |
| 33 | flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv") |
| 34 | metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv") |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame^] | 35 | stubFlagsCSV := &bootImagePath{ctx.Config().HiddenAPIStubFlags()} |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 36 | |
| 37 | ctx.Build(pctx, android.BuildParams{ |
| 38 | Rule: hiddenAPIGenerateCSVRule, |
| 39 | Description: "hiddenapi flags", |
| 40 | Input: classesJar, |
| 41 | Output: flagsCSV, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame^] | 42 | Implicit: stubFlagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 43 | Args: map[string]string{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame^] | 44 | "outFlag": "--write-flags-csv", |
| 45 | "stubAPIFlags": stubFlagsCSV.String(), |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 46 | }, |
| 47 | }) |
| 48 | |
| 49 | ctx.Build(pctx, android.BuildParams{ |
| 50 | Rule: hiddenAPIGenerateCSVRule, |
| 51 | Description: "hiddenapi metadata", |
| 52 | Input: classesJar, |
| 53 | Output: metadataCSV, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame^] | 54 | Implicit: stubFlagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 55 | Args: map[string]string{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame^] | 56 | "outFlag": "--write-metadata-csv", |
| 57 | "stubAPIFlags": stubFlagsCSV.String(), |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 58 | }, |
| 59 | }) |
| 60 | |
| 61 | hiddenAPISaveCSVOutputs(ctx, flagsCSV, metadataCSV) |
| 62 | } |
| 63 | |
| 64 | var 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 && ` + |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 71 | `${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] | 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 | }, |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 78 | }, "flags", "tmpDir", "soongZipFlags") |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 79 | |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 80 | func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.WritablePath, |
| 81 | uncompressDex bool) { |
| 82 | |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 83 | flags := &bootImagePath{ctx.Config().HiddenAPIFlags()} |
| 84 | |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 85 | // 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 := "" |
| 88 | if uncompressDex { |
| 89 | soongZipFlags = "-L 0" |
| 90 | } |
| 91 | |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 92 | ctx.Build(pctx, android.BuildParams{ |
| 93 | Rule: hiddenAPIEncodeDexRule, |
| 94 | Description: "hiddenapi encode dex", |
| 95 | Input: dexInput, |
| 96 | Output: output, |
| 97 | Implicit: flags, |
| 98 | Args: map[string]string{ |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 99 | "flags": flags.String(), |
| 100 | "tmpDir": android.PathForModuleOut(ctx, "hiddenapi", "dex").String(), |
| 101 | "soongZipFlags": soongZipFlags, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 102 | }, |
| 103 | }) |
| 104 | |
| 105 | hiddenAPISaveDexInputs(ctx, dexInput) |
| 106 | } |
| 107 | |
| 108 | const hiddenAPIOutputsKey = "hiddenAPIOutputsKey" |
| 109 | |
| 110 | var hiddenAPIOutputsLock sync.Mutex |
| 111 | |
| 112 | func hiddenAPIGetOutputs(config android.Config) (*android.Paths, *android.Paths, *android.Paths) { |
| 113 | type threePathsPtrs [3]*android.Paths |
| 114 | s := config.Once(hiddenAPIOutputsKey, func() interface{} { |
| 115 | return threePathsPtrs{new(android.Paths), new(android.Paths), new(android.Paths)} |
| 116 | }).(threePathsPtrs) |
| 117 | return s[0], s[1], s[2] |
| 118 | } |
| 119 | |
| 120 | func hiddenAPISaveCSVOutputs(ctx android.ModuleContext, flagsCSV, metadataCSV android.Path) { |
| 121 | flagsCSVList, metadataCSVList, _ := hiddenAPIGetOutputs(ctx.Config()) |
| 122 | |
| 123 | hiddenAPIOutputsLock.Lock() |
| 124 | defer hiddenAPIOutputsLock.Unlock() |
| 125 | |
| 126 | *flagsCSVList = append(*flagsCSVList, flagsCSV) |
| 127 | *metadataCSVList = append(*metadataCSVList, metadataCSV) |
| 128 | } |
| 129 | |
| 130 | func hiddenAPISaveDexInputs(ctx android.ModuleContext, dexInput android.Path) { |
| 131 | _, _, dexInputList := hiddenAPIGetOutputs(ctx.Config()) |
| 132 | |
| 133 | hiddenAPIOutputsLock.Lock() |
| 134 | defer hiddenAPIOutputsLock.Unlock() |
| 135 | |
| 136 | *dexInputList = append(*dexInputList, dexInput) |
| 137 | } |
| 138 | |
| 139 | func init() { |
| 140 | android.RegisterMakeVarsProvider(pctx, hiddenAPIMakeVars) |
| 141 | } |
| 142 | |
| 143 | func hiddenAPIMakeVars(ctx android.MakeVarsContext) { |
| 144 | flagsCSVList, metadataCSVList, dexInputList := hiddenAPIGetOutputs(ctx.Config()) |
| 145 | |
| 146 | export := func(name string, paths *android.Paths) { |
| 147 | s := paths.Strings() |
| 148 | sort.Strings(s) |
| 149 | ctx.Strict(name, strings.Join(s, " ")) |
| 150 | } |
| 151 | |
| 152 | export("SOONG_HIDDENAPI_FLAGS", flagsCSVList) |
| 153 | export("SOONG_HIDDENAPI_GREYLIST_METADATA", metadataCSVList) |
| 154 | export("SOONG_HIDDENAPI_DEX_INPUTS", dexInputList) |
| 155 | } |