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 | |
| 20 | "github.com/google/blueprint" |
| 21 | |
| 22 | "android/soong/android" |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame^] | 23 | "android/soong/java/config" |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 27 | Command: "${config.Class2Greylist} --stub-api-flags ${stubAPIFlags} $in $outFlag $out", |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 28 | CommandDeps: []string{"${config.Class2Greylist}"}, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 29 | }, "outFlag", "stubAPIFlags") |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 30 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame^] | 31 | type hiddenAPI struct { |
| 32 | flagsCSVPath android.Path |
| 33 | metadataCSVPath android.Path |
| 34 | bootDexJarPath android.Path |
| 35 | } |
| 36 | |
| 37 | func (h *hiddenAPI) flagsCSV() android.Path { |
| 38 | return h.flagsCSVPath |
| 39 | } |
| 40 | |
| 41 | func (h *hiddenAPI) metadataCSV() android.Path { |
| 42 | return h.metadataCSVPath |
| 43 | } |
| 44 | |
| 45 | func (h *hiddenAPI) bootDexJar() android.Path { |
| 46 | return h.bootDexJarPath |
| 47 | } |
| 48 | |
| 49 | type hiddenAPIIntf interface { |
| 50 | flagsCSV() android.Path |
| 51 | metadataCSV() android.Path |
| 52 | bootDexJar() android.Path |
| 53 | } |
| 54 | |
| 55 | var _ hiddenAPIIntf = (*hiddenAPI)(nil) |
| 56 | |
| 57 | func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, dexJar android.ModuleOutPath, implementationJar android.Path, |
| 58 | uncompressDex bool) android.ModuleOutPath { |
| 59 | |
| 60 | if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 61 | isBootJar := inList(ctx.ModuleName(), ctx.Config().BootJars()) |
| 62 | if isBootJar || inList(ctx.ModuleName(), config.HiddenAPIExtraAppUsageJars) { |
| 63 | // Derive the greylist from classes jar. |
| 64 | flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv") |
| 65 | metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv") |
| 66 | hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, implementationJar) |
| 67 | h.flagsCSVPath = flagsCSV |
| 68 | h.metadataCSVPath = metadataCSV |
| 69 | } |
| 70 | if isBootJar { |
| 71 | hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", ctx.ModuleName()+".jar") |
| 72 | h.bootDexJarPath = dexJar |
| 73 | hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex) |
| 74 | dexJar = hiddenAPIJar |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return dexJar |
| 79 | } |
| 80 | |
| 81 | func hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV android.WritablePath, |
| 82 | classesJar android.Path) { |
| 83 | |
| 84 | stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 85 | |
| 86 | ctx.Build(pctx, android.BuildParams{ |
| 87 | Rule: hiddenAPIGenerateCSVRule, |
| 88 | Description: "hiddenapi flags", |
| 89 | Input: classesJar, |
| 90 | Output: flagsCSV, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 91 | Implicit: stubFlagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 92 | Args: map[string]string{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 93 | "outFlag": "--write-flags-csv", |
| 94 | "stubAPIFlags": stubFlagsCSV.String(), |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 95 | }, |
| 96 | }) |
| 97 | |
| 98 | ctx.Build(pctx, android.BuildParams{ |
| 99 | Rule: hiddenAPIGenerateCSVRule, |
| 100 | Description: "hiddenapi metadata", |
| 101 | Input: classesJar, |
| 102 | Output: metadataCSV, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 103 | Implicit: stubFlagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 104 | Args: map[string]string{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 105 | "outFlag": "--write-metadata-csv", |
| 106 | "stubAPIFlags": stubFlagsCSV.String(), |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 107 | }, |
| 108 | }) |
| 109 | |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{ |
| 113 | Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output && ` + |
| 114 | `unzip -o -q $in 'classes*.dex' -d $tmpDir/dex-input && ` + |
| 115 | `for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do ` + |
| 116 | ` echo "--input-dex=$${INPUT_DEX}"; ` + |
| 117 | ` echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})"; ` + |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 118 | `done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && ` + |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 119 | `${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] | 120 | `${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" $out $tmpDir/dex.jar $in`, |
| 121 | CommandDeps: []string{ |
| 122 | "${config.HiddenAPI}", |
| 123 | "${config.SoongZipCmd}", |
| 124 | "${config.MergeZipsCmd}", |
| 125 | }, |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 126 | }, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags") |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 127 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame^] | 128 | func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path, |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 129 | uncompressDex bool) { |
| 130 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame^] | 131 | flagsCSV := hiddenAPISingletonPaths(ctx).flags |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 132 | |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 133 | // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed |
| 134 | // in the input it stays uncompressed in the output. |
| 135 | soongZipFlags := "" |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 136 | hiddenapiFlags := "" |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 137 | tmpOutput := output |
| 138 | tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex") |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 139 | if uncompressDex { |
| 140 | soongZipFlags = "-L 0" |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 141 | tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar") |
| 142 | tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned") |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 143 | } |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 144 | // If frameworks/base doesn't exist we must be building with the 'master-art' manifest. |
| 145 | // Disable assertion that all methods/fields have hidden API flags assigned. |
| 146 | if !ctx.Config().FrameworksBaseDirExists(ctx) { |
| 147 | hiddenapiFlags = "--no-force-assign-all" |
| 148 | } |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 149 | |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 150 | ctx.Build(pctx, android.BuildParams{ |
| 151 | Rule: hiddenAPIEncodeDexRule, |
| 152 | Description: "hiddenapi encode dex", |
| 153 | Input: dexInput, |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 154 | Output: tmpOutput, |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame^] | 155 | Implicit: flagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 156 | Args: map[string]string{ |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame^] | 157 | "flagsCsv": flagsCSV.String(), |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 158 | "tmpDir": tmpDir.String(), |
| 159 | "soongZipFlags": soongZipFlags, |
| 160 | "hiddenapiFlags": hiddenapiFlags, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 161 | }, |
| 162 | }) |
| 163 | |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 164 | if uncompressDex { |
| 165 | TransformZipAlign(ctx, output, tmpOutput) |
| 166 | } |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 167 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 168 | |
| 169 | type hiddenAPIPath struct { |
| 170 | path string |
| 171 | } |
| 172 | |
| 173 | var _ android.Path = (*hiddenAPIPath)(nil) |
| 174 | |
| 175 | func (p *hiddenAPIPath) String() string { return p.path } |
| 176 | func (p *hiddenAPIPath) Ext() string { return filepath.Ext(p.path) } |
| 177 | func (p *hiddenAPIPath) Base() string { return filepath.Base(p.path) } |
| 178 | func (p *hiddenAPIPath) Rel() string { return p.path } |