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