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