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