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