Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 | "strings" |
| 19 | |
| 20 | "github.com/google/blueprint" |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 21 | "github.com/google/blueprint/proptools" |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 24 | "android/soong/remoteexec" |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame^] | 27 | var d8, d8RE = remoteexec.MultiCommandStaticRules(pctx, "d8", |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 28 | blueprint.RuleParams{ |
| 29 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame^] | 30 | `$d8Template${config.D8Cmd} ${config.DexFlags} --output $outDir $d8Flags $in && ` + |
| 31 | `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` + |
Colin Cross | 4c03f68 | 2018-07-15 08:16:31 -0700 | [diff] [blame] | 32 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`, |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 33 | CommandDeps: []string{ |
| 34 | "${config.D8Cmd}", |
| 35 | "${config.SoongZipCmd}", |
| 36 | "${config.MergeZipsCmd}", |
| 37 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame^] | 38 | }, map[string]*remoteexec.REParams{ |
| 39 | "$d8Template": &remoteexec.REParams{ |
| 40 | Labels: map[string]string{"type": "compile", "compiler": "d8"}, |
| 41 | Inputs: []string{"${config.D8Jar}"}, |
| 42 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 43 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 44 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 45 | }, |
| 46 | "$zipTemplate": &remoteexec.REParams{ |
| 47 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 48 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 49 | OutputFiles: []string{"$outDir/classes.dex.jar"}, |
| 50 | ExecStrategy: "${config.RED8ExecStrategy}", |
| 51 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 52 | }, |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 53 | }, []string{"outDir", "d8Flags", "zipFlags"}, nil) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 54 | |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame^] | 55 | var r8, r8RE = remoteexec.MultiCommandStaticRules(pctx, "r8", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 56 | blueprint.RuleParams{ |
| 57 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
Søren Gjesse | 0e84935 | 2018-08-22 16:16:23 +0200 | [diff] [blame] | 58 | `rm -f "$outDict" && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame^] | 59 | `$r8Template${config.R8Cmd} ${config.DexFlags} -injars $in --output $outDir ` + |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 60 | `--force-proguard-compatibility ` + |
Søren Gjesse | 24f1702 | 2018-09-14 15:20:42 +0200 | [diff] [blame] | 61 | `--no-data-resources ` + |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 62 | `-printmapping $outDict ` + |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 63 | `$r8Flags && ` + |
Søren Gjesse | 0e84935 | 2018-08-22 16:16:23 +0200 | [diff] [blame] | 64 | `touch "$outDict" && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame^] | 65 | `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` + |
Colin Cross | 4c03f68 | 2018-07-15 08:16:31 -0700 | [diff] [blame] | 66 | `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 67 | CommandDeps: []string{ |
| 68 | "${config.R8Cmd}", |
| 69 | "${config.SoongZipCmd}", |
| 70 | "${config.MergeZipsCmd}", |
| 71 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame^] | 72 | }, map[string]*remoteexec.REParams{ |
| 73 | "$r8Template": &remoteexec.REParams{ |
| 74 | Labels: map[string]string{"type": "compile", "compiler": "r8"}, |
| 75 | Inputs: []string{"$implicits", "${config.R8Jar}"}, |
| 76 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 77 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 78 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 79 | }, |
| 80 | "$zipTemplate": &remoteexec.REParams{ |
| 81 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 82 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 83 | OutputFiles: []string{"$outDir/classes.dex.jar"}, |
| 84 | ExecStrategy: "${config.RER8ExecStrategy}", |
| 85 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 86 | }, |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 87 | }, []string{"outDir", "outDict", "r8Flags", "zipFlags"}, []string{"implicits"}) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 88 | |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 89 | func (j *Module) dexCommonFlags(ctx android.ModuleContext) []string { |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 90 | flags := j.deviceProperties.Dxflags |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 91 | // Translate all the DX flags to D8 ones until all the build files have been migrated |
| 92 | // to D8 flags. See: b/69377755 |
| 93 | flags = android.RemoveListFromList(flags, |
| 94 | []string{"--core-library", "--dex", "--multi-dex"}) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 95 | |
| 96 | if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" { |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 97 | flags = append(flags, "--debug") |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" { |
| 101 | flags = append(flags, |
| 102 | "--debug", |
| 103 | "--verbose") |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 106 | minSdkVersion, err := j.minSdkVersion().effectiveVersion(ctx) |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 107 | if err != nil { |
| 108 | ctx.PropertyErrorf("min_sdk_version", "%s", err) |
| 109 | } |
| 110 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 111 | flags = append(flags, "--min-api "+minSdkVersion.asNumberString()) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 112 | return flags |
| 113 | } |
| 114 | |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 115 | func (j *Module) d8Flags(ctx android.ModuleContext, flags javaBuilderFlags) ([]string, android.Paths) { |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 116 | d8Flags := j.dexCommonFlags(ctx) |
| 117 | |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 118 | d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...) |
| 119 | d8Flags = append(d8Flags, flags.classpath.FormRepeatedClassPath("--lib ")...) |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 120 | |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 121 | var d8Deps android.Paths |
| 122 | d8Deps = append(d8Deps, flags.bootClasspath...) |
| 123 | d8Deps = append(d8Deps, flags.classpath...) |
| 124 | |
| 125 | return d8Flags, d8Deps |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 128 | func (j *Module) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) { |
| 129 | opt := j.deviceProperties.Optimize |
| 130 | |
| 131 | // When an app contains references to APIs that are not in the SDK specified by |
| 132 | // its LOCAL_SDK_VERSION for example added by support library or by runtime |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 133 | // classes added by desugaring, we artifically raise the "SDK version" "linked" by |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 134 | // ProGuard, to |
| 135 | // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version. |
| 136 | // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version. |
| 137 | // See b/20667396 |
| 138 | var proguardRaiseDeps classpath |
| 139 | ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(dep android.Module) { |
| 140 | proguardRaiseDeps = append(proguardRaiseDeps, dep.(Dependency).HeaderJars()...) |
| 141 | }) |
| 142 | |
Colin Cross | ffb657e | 2018-09-21 12:29:22 -0700 | [diff] [blame] | 143 | r8Flags = append(r8Flags, j.dexCommonFlags(ctx)...) |
| 144 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 145 | r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars")) |
| 146 | r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars")) |
| 147 | r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars")) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 148 | |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 149 | r8Deps = append(r8Deps, proguardRaiseDeps...) |
| 150 | r8Deps = append(r8Deps, flags.bootClasspath...) |
| 151 | r8Deps = append(r8Deps, flags.classpath...) |
| 152 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 153 | flagFiles := android.Paths{ |
| 154 | android.PathForSource(ctx, "build/make/core/proguard.flags"), |
| 155 | } |
| 156 | |
Colin Cross | 3144dfc | 2018-01-03 15:06:47 -0800 | [diff] [blame] | 157 | if j.shouldInstrumentStatic(ctx) { |
| 158 | flagFiles = append(flagFiles, |
| 159 | android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags")) |
| 160 | } |
| 161 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 162 | flagFiles = append(flagFiles, j.extraProguardFlagFiles...) |
| 163 | // TODO(ccross): static android library proguard files |
| 164 | |
Colin Cross | bd1cef5 | 2018-08-13 10:28:18 -0700 | [diff] [blame] | 165 | flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, j.deviceProperties.Optimize.Proguard_flags_files)...) |
| 166 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 167 | r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include ")) |
| 168 | r8Deps = append(r8Deps, flagFiles...) |
| 169 | |
| 170 | // TODO(b/70942988): This is included from build/make/core/proguard.flags |
| 171 | r8Deps = append(r8Deps, android.PathForSource(ctx, |
| 172 | "build/make/core/proguard_basic_keeps.flags")) |
| 173 | |
| 174 | r8Flags = append(r8Flags, j.deviceProperties.Optimize.Proguard_flags...) |
| 175 | |
| 176 | // TODO(ccross): Don't shrink app instrumentation tests by default. |
| 177 | if !Bool(opt.Shrink) { |
| 178 | r8Flags = append(r8Flags, "-dontshrink") |
| 179 | } |
| 180 | |
| 181 | if !Bool(opt.Optimize) { |
| 182 | r8Flags = append(r8Flags, "-dontoptimize") |
| 183 | } |
| 184 | |
| 185 | // TODO(ccross): error if obufscation + app instrumentation test. |
| 186 | if !Bool(opt.Obfuscate) { |
| 187 | r8Flags = append(r8Flags, "-dontobfuscate") |
| 188 | } |
Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 189 | // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the |
| 190 | // dictionary of the app and move the app from libraryjars to injars. |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 191 | |
Jaewoong Jung | 1d6eb68 | 2018-11-29 15:08:44 -0800 | [diff] [blame] | 192 | // Don't strip out debug information for eng builds. |
| 193 | if ctx.Config().Eng() { |
| 194 | r8Flags = append(r8Flags, "--debug") |
| 195 | } |
| 196 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 197 | return r8Flags, r8Deps |
| 198 | } |
| 199 | |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 200 | func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags, |
Colin Cross | 3063b78 | 2018-08-15 11:19:12 -0700 | [diff] [blame] | 201 | classesJar android.Path, jarName string) android.ModuleOutPath { |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 202 | |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 203 | useR8 := j.deviceProperties.EffectiveOptimizeEnabled() |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 204 | |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 205 | // Compile classes.jar into classes.dex and then javalib.jar |
| 206 | javalibJar := android.PathForModuleOut(ctx, "dex", jarName) |
| 207 | outDir := android.PathForModuleOut(ctx, "dex") |
| 208 | |
Sasha Smundak | d3cf4ee | 2019-02-15 10:14:23 -0800 | [diff] [blame] | 209 | zipFlags := "--ignore_missing_files" |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 210 | if proptools.Bool(j.deviceProperties.Uncompress_dex) { |
Sasha Smundak | d3cf4ee | 2019-02-15 10:14:23 -0800 | [diff] [blame] | 211 | zipFlags += " -L 0" |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 214 | if useR8 { |
Colin Cross | c0c664c | 2018-05-16 16:47:21 -0700 | [diff] [blame] | 215 | proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary") |
| 216 | j.proguardDictionary = proguardDictionary |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 217 | r8Flags, r8Deps := j.r8Flags(ctx, flags) |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 218 | rule := r8 |
| 219 | args := map[string]string{ |
| 220 | "r8Flags": strings.Join(r8Flags, " "), |
| 221 | "zipFlags": zipFlags, |
| 222 | "outDict": j.proguardDictionary.String(), |
| 223 | "outDir": outDir.String(), |
| 224 | } |
| 225 | if ctx.Config().IsEnvTrue("RBE_R8") { |
| 226 | rule = r8RE |
| 227 | args["implicits"] = strings.Join(r8Deps.Strings(), ",") |
| 228 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 229 | ctx.Build(pctx, android.BuildParams{ |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 230 | Rule: rule, |
Colin Cross | c0c664c | 2018-05-16 16:47:21 -0700 | [diff] [blame] | 231 | Description: "r8", |
| 232 | Output: javalibJar, |
| 233 | ImplicitOutput: proguardDictionary, |
| 234 | Input: classesJar, |
| 235 | Implicits: r8Deps, |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 236 | Args: args, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 237 | }) |
| 238 | } else { |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 239 | d8Flags, d8Deps := j.d8Flags(ctx, flags) |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 240 | rule := d8 |
| 241 | if ctx.Config().IsEnvTrue("RBE_D8") { |
| 242 | rule = d8RE |
| 243 | } |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 244 | ctx.Build(pctx, android.BuildParams{ |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 245 | Rule: rule, |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 246 | Description: "d8", |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 247 | Output: javalibJar, |
| 248 | Input: classesJar, |
Colin Cross | 6dab9bd | 2018-09-28 08:06:24 -0700 | [diff] [blame] | 249 | Implicits: d8Deps, |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 250 | Args: map[string]string{ |
Colin Cross | 5a0dcd5 | 2018-10-05 14:20:06 -0700 | [diff] [blame] | 251 | "d8Flags": strings.Join(d8Flags, " "), |
| 252 | "zipFlags": zipFlags, |
| 253 | "outDir": outDir.String(), |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 254 | }, |
| 255 | }) |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 256 | } |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 257 | if proptools.Bool(j.deviceProperties.Uncompress_dex) { |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 258 | alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", jarName) |
| 259 | TransformZipAlign(ctx, alignedJavalibJar, javalibJar) |
| 260 | javalibJar = alignedJavalibJar |
| 261 | } |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 262 | |
Colin Cross | f0056cb | 2017-12-22 15:56:08 -0800 | [diff] [blame] | 263 | return javalibJar |
| 264 | } |