blob: fd7110f9f1ecc6d7f979aa14bbf6c8f5ec2d7e71 [file] [log] [blame]
Colin Crossf0056cb2017-12-22 15:56:08 -08001// 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
15package java
16
17import (
18 "strings"
19
20 "github.com/google/blueprint"
David Srbecky98c71222020-05-20 22:20:28 +010021 "github.com/google/blueprint/proptools"
Colin Crossf0056cb2017-12-22 15:56:08 -080022
23 "android/soong/android"
Treehugger Robotac80afa2020-04-28 19:46:33 +000024 "android/soong/remoteexec"
Colin Crossf0056cb2017-12-22 15:56:08 -080025)
26
Kousik Kumarc05da0a2020-05-20 11:27:16 -070027var d8, d8RE = remoteexec.MultiCommandStaticRules(pctx, "d8",
Colin Crossf0056cb2017-12-22 15:56:08 -080028 blueprint.RuleParams{
29 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Kousik Kumarc05da0a2020-05-20 11:27:16 -070030 `$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 Cross4c03f682018-07-15 08:16:31 -070032 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
Colin Crossf0056cb2017-12-22 15:56:08 -080033 CommandDeps: []string{
34 "${config.D8Cmd}",
35 "${config.SoongZipCmd}",
36 "${config.MergeZipsCmd}",
37 },
Kousik Kumarc05da0a2020-05-20 11:27:16 -070038 }, 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 },
Treehugger Robotac80afa2020-04-28 19:46:33 +000053 }, []string{"outDir", "d8Flags", "zipFlags"}, nil)
Colin Crossf0056cb2017-12-22 15:56:08 -080054
Kousik Kumarc05da0a2020-05-20 11:27:16 -070055var r8, r8RE = remoteexec.MultiCommandStaticRules(pctx, "r8",
Colin Cross66dbc0b2017-12-28 12:23:20 -080056 blueprint.RuleParams{
57 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Crosse064cf22020-08-14 17:39:29 -070058 `rm -f "$outDict" && rm -rf "${outUsageDir}" && ` +
59 `mkdir -p $$(dirname ${outUsage}) && ` +
Kousik Kumarc05da0a2020-05-20 11:27:16 -070060 `$r8Template${config.R8Cmd} ${config.DexFlags} -injars $in --output $outDir ` +
Colin Cross66dbc0b2017-12-28 12:23:20 -080061 `--force-proguard-compatibility ` +
Søren Gjesse24f17022018-09-14 15:20:42 +020062 `--no-data-resources ` +
Colin Crosse064cf22020-08-14 17:39:29 -070063 `-printmapping ${outDict} ` +
64 `-printusage ${outUsage} ` +
Colin Crossffb657e2018-09-21 12:29:22 -070065 `$r8Flags && ` +
Colin Crosse064cf22020-08-14 17:39:29 -070066 `touch "${outDict}" "${outUsage}" && ` +
67 `${config.SoongZipCmd} -o ${outUsageZip} -C ${outUsageDir} -f ${outUsage} && ` +
68 `rm -rf ${outUsageDir} && ` +
Kousik Kumarc05da0a2020-05-20 11:27:16 -070069 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Colin Cross4c03f682018-07-15 08:16:31 -070070 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
Colin Cross66dbc0b2017-12-28 12:23:20 -080071 CommandDeps: []string{
72 "${config.R8Cmd}",
73 "${config.SoongZipCmd}",
74 "${config.MergeZipsCmd}",
75 },
Kousik Kumarc05da0a2020-05-20 11:27:16 -070076 }, map[string]*remoteexec.REParams{
77 "$r8Template": &remoteexec.REParams{
78 Labels: map[string]string{"type": "compile", "compiler": "r8"},
79 Inputs: []string{"$implicits", "${config.R8Jar}"},
80 ExecStrategy: "${config.RER8ExecStrategy}",
81 ToolchainInputs: []string{"${config.JavaCmd}"},
82 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
83 },
84 "$zipTemplate": &remoteexec.REParams{
85 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
86 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
87 OutputFiles: []string{"$outDir/classes.dex.jar"},
88 ExecStrategy: "${config.RER8ExecStrategy}",
89 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
90 },
Colin Crosse064cf22020-08-14 17:39:29 -070091 "$zipUsageTemplate": &remoteexec.REParams{
92 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
93 Inputs: []string{"${config.SoongZipCmd}", "${outUsage}"},
94 OutputFiles: []string{"${outUsageZip}"},
95 ExecStrategy: "${config.RER8ExecStrategy}",
96 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
97 },
98 }, []string{"outDir", "outDict", "outUsage", "outUsageZip", "outUsageDir",
99 "r8Flags", "zipFlags"}, []string{"implicits"})
Colin Cross66dbc0b2017-12-28 12:23:20 -0800100
Colin Crossffb657e2018-09-21 12:29:22 -0700101func (j *Module) dexCommonFlags(ctx android.ModuleContext) []string {
Colin Crossf0056cb2017-12-22 15:56:08 -0800102 flags := j.deviceProperties.Dxflags
Colin Crossbafb8972018-06-06 21:46:32 +0000103 // Translate all the DX flags to D8 ones until all the build files have been migrated
104 // to D8 flags. See: b/69377755
105 flags = android.RemoveListFromList(flags,
106 []string{"--core-library", "--dex", "--multi-dex"})
Colin Crossf0056cb2017-12-22 15:56:08 -0800107
108 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Crossbafb8972018-06-06 21:46:32 +0000109 flags = append(flags, "--debug")
Colin Crossf0056cb2017-12-22 15:56:08 -0800110 }
111
112 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
113 flags = append(flags,
114 "--debug",
115 "--verbose")
Colin Crossf0056cb2017-12-22 15:56:08 -0800116 }
117
Jiyong Park6a927c42020-01-21 02:03:43 +0900118 minSdkVersion, err := j.minSdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700119 if err != nil {
120 ctx.PropertyErrorf("min_sdk_version", "%s", err)
121 }
122
Jiyong Park6a927c42020-01-21 02:03:43 +0900123 flags = append(flags, "--min-api "+minSdkVersion.asNumberString())
Colin Crossf0056cb2017-12-22 15:56:08 -0800124 return flags
125}
126
Colin Cross6dab9bd2018-09-28 08:06:24 -0700127func (j *Module) d8Flags(ctx android.ModuleContext, flags javaBuilderFlags) ([]string, android.Paths) {
Colin Crossffb657e2018-09-21 12:29:22 -0700128 d8Flags := j.dexCommonFlags(ctx)
129
Colin Crossc2557d12019-10-31 15:22:57 -0700130 d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
131 d8Flags = append(d8Flags, flags.classpath.FormRepeatedClassPath("--lib ")...)
Colin Crossffb657e2018-09-21 12:29:22 -0700132
Colin Cross6dab9bd2018-09-28 08:06:24 -0700133 var d8Deps android.Paths
134 d8Deps = append(d8Deps, flags.bootClasspath...)
135 d8Deps = append(d8Deps, flags.classpath...)
136
137 return d8Flags, d8Deps
Colin Crossffb657e2018-09-21 12:29:22 -0700138}
139
Colin Cross66dbc0b2017-12-28 12:23:20 -0800140func (j *Module) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
141 opt := j.deviceProperties.Optimize
142
143 // When an app contains references to APIs that are not in the SDK specified by
144 // its LOCAL_SDK_VERSION for example added by support library or by runtime
Colin Crossbafb8972018-06-06 21:46:32 +0000145 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -0800146 // ProGuard, to
147 // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
148 // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
149 // See b/20667396
150 var proguardRaiseDeps classpath
151 ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(dep android.Module) {
152 proguardRaiseDeps = append(proguardRaiseDeps, dep.(Dependency).HeaderJars()...)
153 })
154
Colin Crossffb657e2018-09-21 12:29:22 -0700155 r8Flags = append(r8Flags, j.dexCommonFlags(ctx)...)
156
Colin Cross66dbc0b2017-12-28 12:23:20 -0800157 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
158 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
159 r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars"))
Colin Cross66dbc0b2017-12-28 12:23:20 -0800160
Colin Cross6dab9bd2018-09-28 08:06:24 -0700161 r8Deps = append(r8Deps, proguardRaiseDeps...)
162 r8Deps = append(r8Deps, flags.bootClasspath...)
163 r8Deps = append(r8Deps, flags.classpath...)
164
Colin Cross66dbc0b2017-12-28 12:23:20 -0800165 flagFiles := android.Paths{
166 android.PathForSource(ctx, "build/make/core/proguard.flags"),
167 }
168
Colin Cross3144dfc2018-01-03 15:06:47 -0800169 if j.shouldInstrumentStatic(ctx) {
170 flagFiles = append(flagFiles,
171 android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
172 }
173
Colin Cross66dbc0b2017-12-28 12:23:20 -0800174 flagFiles = append(flagFiles, j.extraProguardFlagFiles...)
175 // TODO(ccross): static android library proguard files
176
Colin Crossbd1cef52018-08-13 10:28:18 -0700177 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, j.deviceProperties.Optimize.Proguard_flags_files)...)
178
Colin Cross66dbc0b2017-12-28 12:23:20 -0800179 r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
180 r8Deps = append(r8Deps, flagFiles...)
181
182 // TODO(b/70942988): This is included from build/make/core/proguard.flags
183 r8Deps = append(r8Deps, android.PathForSource(ctx,
184 "build/make/core/proguard_basic_keeps.flags"))
185
186 r8Flags = append(r8Flags, j.deviceProperties.Optimize.Proguard_flags...)
187
188 // TODO(ccross): Don't shrink app instrumentation tests by default.
189 if !Bool(opt.Shrink) {
190 r8Flags = append(r8Flags, "-dontshrink")
191 }
192
193 if !Bool(opt.Optimize) {
194 r8Flags = append(r8Flags, "-dontoptimize")
195 }
196
197 // TODO(ccross): error if obufscation + app instrumentation test.
198 if !Bool(opt.Obfuscate) {
199 r8Flags = append(r8Flags, "-dontobfuscate")
200 }
Colin Cross4b964c02018-10-15 16:18:06 -0700201 // TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
202 // dictionary of the app and move the app from libraryjars to injars.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800203
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800204 // Don't strip out debug information for eng builds.
205 if ctx.Config().Eng() {
206 r8Flags = append(r8Flags, "--debug")
207 }
208
Colin Cross66dbc0b2017-12-28 12:23:20 -0800209 return r8Flags, r8Deps
210}
211
Colin Crossf0056cb2017-12-22 15:56:08 -0800212func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -0700213 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crossf0056cb2017-12-22 15:56:08 -0800214
Sasha Smundak2057f822019-04-16 17:16:58 -0700215 useR8 := j.deviceProperties.EffectiveOptimizeEnabled()
Colin Crossf0056cb2017-12-22 15:56:08 -0800216
Colin Crossf0056cb2017-12-22 15:56:08 -0800217 // Compile classes.jar into classes.dex and then javalib.jar
218 javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
219 outDir := android.PathForModuleOut(ctx, "dex")
220
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800221 zipFlags := "--ignore_missing_files"
David Srbecky98c71222020-05-20 22:20:28 +0100222 if proptools.Bool(j.deviceProperties.Uncompress_dex) {
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800223 zipFlags += " -L 0"
Colin Cross5a0dcd52018-10-05 14:20:06 -0700224 }
225
Colin Cross66dbc0b2017-12-28 12:23:20 -0800226 if useR8 {
Colin Crossc0c664c2018-05-16 16:47:21 -0700227 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
228 j.proguardDictionary = proguardDictionary
Colin Crosse064cf22020-08-14 17:39:29 -0700229 proguardUsageDir := android.PathForModuleOut(ctx, "proguard_usage")
230 proguardUsage := proguardUsageDir.Join(ctx, ctx.Namespace().Path,
231 android.ModuleNameWithPossibleOverride(ctx), "unused.txt")
232 proguardUsageZip := android.PathForModuleOut(ctx, "proguard_usage.zip")
233 j.proguardUsageZip = android.OptionalPathForPath(proguardUsageZip)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800234 r8Flags, r8Deps := j.r8Flags(ctx, flags)
Treehugger Robotac80afa2020-04-28 19:46:33 +0000235 rule := r8
236 args := map[string]string{
Colin Crosse064cf22020-08-14 17:39:29 -0700237 "r8Flags": strings.Join(r8Flags, " "),
238 "zipFlags": zipFlags,
239 "outDict": j.proguardDictionary.String(),
240 "outUsageDir": proguardUsageDir.String(),
241 "outUsage": proguardUsage.String(),
242 "outUsageZip": proguardUsageZip.String(),
243 "outDir": outDir.String(),
Treehugger Robotac80afa2020-04-28 19:46:33 +0000244 }
245 if ctx.Config().IsEnvTrue("RBE_R8") {
246 rule = r8RE
247 args["implicits"] = strings.Join(r8Deps.Strings(), ",")
248 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800249 ctx.Build(pctx, android.BuildParams{
Colin Crosse064cf22020-08-14 17:39:29 -0700250 Rule: rule,
251 Description: "r8",
252 Output: javalibJar,
253 ImplicitOutputs: android.WritablePaths{proguardDictionary, proguardUsageZip},
254 Input: classesJar,
255 Implicits: r8Deps,
256 Args: args,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800257 })
258 } else {
Colin Cross6dab9bd2018-09-28 08:06:24 -0700259 d8Flags, d8Deps := j.d8Flags(ctx, flags)
Treehugger Robotac80afa2020-04-28 19:46:33 +0000260 rule := d8
261 if ctx.Config().IsEnvTrue("RBE_D8") {
262 rule = d8RE
263 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800264 ctx.Build(pctx, android.BuildParams{
Treehugger Robotac80afa2020-04-28 19:46:33 +0000265 Rule: rule,
Colin Crossbafb8972018-06-06 21:46:32 +0000266 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800267 Output: javalibJar,
268 Input: classesJar,
Colin Cross6dab9bd2018-09-28 08:06:24 -0700269 Implicits: d8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800270 Args: map[string]string{
Colin Cross5a0dcd52018-10-05 14:20:06 -0700271 "d8Flags": strings.Join(d8Flags, " "),
272 "zipFlags": zipFlags,
273 "outDir": outDir.String(),
Colin Cross66dbc0b2017-12-28 12:23:20 -0800274 },
275 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800276 }
David Srbecky98c71222020-05-20 22:20:28 +0100277 if proptools.Bool(j.deviceProperties.Uncompress_dex) {
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000278 alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", jarName)
279 TransformZipAlign(ctx, alignedJavalibJar, javalibJar)
280 javalibJar = alignedJavalibJar
281 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800282
Colin Crossf0056cb2017-12-22 15:56:08 -0800283 return javalibJar
284}