blob: e2a8e7eae3a34ff580b7fbf16cd76559a83bbedc [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 Srbeckye033cba2020-05-20 22:20:28 +010021 "github.com/google/blueprint/proptools"
Colin Crossf0056cb2017-12-22 15:56:08 -080022
23 "android/soong/android"
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040024 "android/soong/remoteexec"
Colin Crossf0056cb2017-12-22 15:56:08 -080025)
26
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040027var d8, d8RE = remoteexec.StaticRules(pctx, "d8",
Colin Crossf0056cb2017-12-22 15:56:08 -080028 blueprint.RuleParams{
29 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040030 `$reTemplate${config.D8Cmd} ${config.DexFlags} --output $outDir $d8Flags $in && ` +
Colin Cross5a0dcd52018-10-05 14:20:06 -070031 `${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 },
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040038 }, &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 Crossf0056cb2017-12-22 15:56:08 -080045
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040046var r8, r8RE = remoteexec.StaticRules(pctx, "r8",
Colin Cross66dbc0b2017-12-28 12:23:20 -080047 blueprint.RuleParams{
48 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Søren Gjesse0e849352018-08-22 16:16:23 +020049 `rm -f "$outDict" && ` +
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040050 `$reTemplate${config.R8Cmd} ${config.DexFlags} -injars $in --output $outDir ` +
Colin Cross66dbc0b2017-12-28 12:23:20 -080051 `--force-proguard-compatibility ` +
Søren Gjesse24f17022018-09-14 15:20:42 +020052 `--no-data-resources ` +
Colin Cross66dbc0b2017-12-28 12:23:20 -080053 `-printmapping $outDict ` +
Colin Crossffb657e2018-09-21 12:29:22 -070054 `$r8Flags && ` +
Søren Gjesse0e849352018-08-22 16:16:23 +020055 `touch "$outDict" && ` +
Colin Cross5a0dcd52018-10-05 14:20:06 -070056 `${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Colin Cross4c03f682018-07-15 08:16:31 -070057 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
Colin Cross66dbc0b2017-12-28 12:23:20 -080058 CommandDeps: []string{
59 "${config.R8Cmd}",
60 "${config.SoongZipCmd}",
61 "${config.MergeZipsCmd}",
62 },
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040063 }, &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 Cross66dbc0b2017-12-28 12:23:20 -080070
Colin Crossffb657e2018-09-21 12:29:22 -070071func (j *Module) dexCommonFlags(ctx android.ModuleContext) []string {
Colin Crossf0056cb2017-12-22 15:56:08 -080072 flags := j.deviceProperties.Dxflags
Colin Crossbafb8972018-06-06 21:46:32 +000073 // 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 Crossf0056cb2017-12-22 15:56:08 -080077
78 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Crossbafb8972018-06-06 21:46:32 +000079 flags = append(flags, "--debug")
Colin Crossf0056cb2017-12-22 15:56:08 -080080 }
81
82 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
83 flags = append(flags,
84 "--debug",
85 "--verbose")
Colin Crossf0056cb2017-12-22 15:56:08 -080086 }
87
Jiyong Park6a927c42020-01-21 02:03:43 +090088 minSdkVersion, err := j.minSdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -070089 if err != nil {
90 ctx.PropertyErrorf("min_sdk_version", "%s", err)
91 }
92
Jiyong Park6a927c42020-01-21 02:03:43 +090093 flags = append(flags, "--min-api "+minSdkVersion.asNumberString())
Colin Crossf0056cb2017-12-22 15:56:08 -080094 return flags
95}
96
Colin Cross6dab9bd2018-09-28 08:06:24 -070097func (j *Module) d8Flags(ctx android.ModuleContext, flags javaBuilderFlags) ([]string, android.Paths) {
Colin Crossffb657e2018-09-21 12:29:22 -070098 d8Flags := j.dexCommonFlags(ctx)
99
Colin Crossc2557d12019-10-31 15:22:57 -0700100 d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
101 d8Flags = append(d8Flags, flags.classpath.FormRepeatedClassPath("--lib ")...)
Colin Crossffb657e2018-09-21 12:29:22 -0700102
Colin Cross6dab9bd2018-09-28 08:06:24 -0700103 var d8Deps android.Paths
104 d8Deps = append(d8Deps, flags.bootClasspath...)
105 d8Deps = append(d8Deps, flags.classpath...)
106
107 return d8Flags, d8Deps
Colin Crossffb657e2018-09-21 12:29:22 -0700108}
109
Colin Cross66dbc0b2017-12-28 12:23:20 -0800110func (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 Crossbafb8972018-06-06 21:46:32 +0000115 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -0800116 // 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 Crossffb657e2018-09-21 12:29:22 -0700125 r8Flags = append(r8Flags, j.dexCommonFlags(ctx)...)
126
Colin Cross66dbc0b2017-12-28 12:23:20 -0800127 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
128 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
129 r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars"))
Colin Cross66dbc0b2017-12-28 12:23:20 -0800130
Colin Cross6dab9bd2018-09-28 08:06:24 -0700131 r8Deps = append(r8Deps, proguardRaiseDeps...)
132 r8Deps = append(r8Deps, flags.bootClasspath...)
133 r8Deps = append(r8Deps, flags.classpath...)
134
Colin Cross66dbc0b2017-12-28 12:23:20 -0800135 flagFiles := android.Paths{
136 android.PathForSource(ctx, "build/make/core/proguard.flags"),
137 }
138
Colin Cross3144dfc2018-01-03 15:06:47 -0800139 if j.shouldInstrumentStatic(ctx) {
140 flagFiles = append(flagFiles,
141 android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
142 }
143
Colin Cross66dbc0b2017-12-28 12:23:20 -0800144 flagFiles = append(flagFiles, j.extraProguardFlagFiles...)
145 // TODO(ccross): static android library proguard files
146
Colin Crossbd1cef52018-08-13 10:28:18 -0700147 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, j.deviceProperties.Optimize.Proguard_flags_files)...)
148
Colin Cross66dbc0b2017-12-28 12:23:20 -0800149 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 Cross4b964c02018-10-15 16:18:06 -0700171 // 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 Cross66dbc0b2017-12-28 12:23:20 -0800173
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800174 // Don't strip out debug information for eng builds.
175 if ctx.Config().Eng() {
176 r8Flags = append(r8Flags, "--debug")
177 }
178
Colin Cross66dbc0b2017-12-28 12:23:20 -0800179 return r8Flags, r8Deps
180}
181
Colin Crossf0056cb2017-12-22 15:56:08 -0800182func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -0700183 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crossf0056cb2017-12-22 15:56:08 -0800184
Sasha Smundak2057f822019-04-16 17:16:58 -0700185 useR8 := j.deviceProperties.EffectiveOptimizeEnabled()
Colin Crossf0056cb2017-12-22 15:56:08 -0800186
Colin Crossf0056cb2017-12-22 15:56:08 -0800187 // 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 Smundakd3cf4ee2019-02-15 10:14:23 -0800191 zipFlags := "--ignore_missing_files"
David Srbeckye033cba2020-05-20 22:20:28 +0100192 if proptools.Bool(j.deviceProperties.Uncompress_dex) {
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800193 zipFlags += " -L 0"
Colin Cross5a0dcd52018-10-05 14:20:06 -0700194 }
195
Colin Cross66dbc0b2017-12-28 12:23:20 -0800196 if useR8 {
Colin Crossc0c664c2018-05-16 16:47:21 -0700197 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
198 j.proguardDictionary = proguardDictionary
Colin Cross66dbc0b2017-12-28 12:23:20 -0800199 r8Flags, r8Deps := j.r8Flags(ctx, flags)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400200 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 Cross66dbc0b2017-12-28 12:23:20 -0800211 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400212 Rule: rule,
Colin Crossc0c664c2018-05-16 16:47:21 -0700213 Description: "r8",
214 Output: javalibJar,
215 ImplicitOutput: proguardDictionary,
216 Input: classesJar,
217 Implicits: r8Deps,
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400218 Args: args,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800219 })
220 } else {
Colin Cross6dab9bd2018-09-28 08:06:24 -0700221 d8Flags, d8Deps := j.d8Flags(ctx, flags)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400222 rule := d8
223 if ctx.Config().IsEnvTrue("RBE_D8") {
224 rule = d8RE
225 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800226 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400227 Rule: rule,
Colin Crossbafb8972018-06-06 21:46:32 +0000228 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800229 Output: javalibJar,
230 Input: classesJar,
Colin Cross6dab9bd2018-09-28 08:06:24 -0700231 Implicits: d8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800232 Args: map[string]string{
Colin Cross5a0dcd52018-10-05 14:20:06 -0700233 "d8Flags": strings.Join(d8Flags, " "),
234 "zipFlags": zipFlags,
235 "outDir": outDir.String(),
Colin Cross66dbc0b2017-12-28 12:23:20 -0800236 },
237 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800238 }
David Srbeckye033cba2020-05-20 22:20:28 +0100239 if proptools.Bool(j.deviceProperties.Uncompress_dex) {
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000240 alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", jarName)
241 TransformZipAlign(ctx, alignedJavalibJar, javalibJar)
242 javalibJar = alignedJavalibJar
243 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800244
Colin Crossf0056cb2017-12-22 15:56:08 -0800245 return javalibJar
246}