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