blob: 9e61e95ade9f29009312a9139191943c58531af5 [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
Kousik Kumar366afc52020-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 Kumar366afc52020-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 Kumar366afc52020-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 },
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040053 }, []string{"outDir", "d8Flags", "zipFlags"}, nil)
Colin Crossf0056cb2017-12-22 15:56:08 -080054
Kousik Kumar366afc52020-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" && ` +
Søren Gjesse0e849352018-08-22 16:16:23 +020058 `rm -f "$outDict" && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -070059 `$r8Template${config.R8Cmd} ${config.DexFlags} -injars $in --output $outDir ` +
Colin Cross66dbc0b2017-12-28 12:23:20 -080060 `--force-proguard-compatibility ` +
Søren Gjesse24f17022018-09-14 15:20:42 +020061 `--no-data-resources ` +
Colin Cross66dbc0b2017-12-28 12:23:20 -080062 `-printmapping $outDict ` +
Colin Crossffb657e2018-09-21 12:29:22 -070063 `$r8Flags && ` +
Søren Gjesse0e849352018-08-22 16:16:23 +020064 `touch "$outDict" && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -070065 `$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
Colin Cross4c03f682018-07-15 08:16:31 -070066 `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
Colin Cross66dbc0b2017-12-28 12:23:20 -080067 CommandDeps: []string{
68 "${config.R8Cmd}",
69 "${config.SoongZipCmd}",
70 "${config.MergeZipsCmd}",
71 },
Kousik Kumar366afc52020-05-20 11:27:16 -070072 }, 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 Medhat1dcc27e2020-04-21 21:36:23 -040087 }, []string{"outDir", "outDict", "r8Flags", "zipFlags"}, []string{"implicits"})
Colin Cross66dbc0b2017-12-28 12:23:20 -080088
Colin Crossffb657e2018-09-21 12:29:22 -070089func (j *Module) dexCommonFlags(ctx android.ModuleContext) []string {
Colin Crossf0056cb2017-12-22 15:56:08 -080090 flags := j.deviceProperties.Dxflags
Colin Crossbafb8972018-06-06 21:46:32 +000091 // 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 Crossf0056cb2017-12-22 15:56:08 -080095
96 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Crossbafb8972018-06-06 21:46:32 +000097 flags = append(flags, "--debug")
Colin Crossf0056cb2017-12-22 15:56:08 -080098 }
99
100 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
101 flags = append(flags,
102 "--debug",
103 "--verbose")
Colin Crossf0056cb2017-12-22 15:56:08 -0800104 }
105
Jiyong Park6a927c42020-01-21 02:03:43 +0900106 minSdkVersion, err := j.minSdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700107 if err != nil {
108 ctx.PropertyErrorf("min_sdk_version", "%s", err)
109 }
110
Jiyong Park6a927c42020-01-21 02:03:43 +0900111 flags = append(flags, "--min-api "+minSdkVersion.asNumberString())
Colin Crossf0056cb2017-12-22 15:56:08 -0800112 return flags
113}
114
Colin Cross6dab9bd2018-09-28 08:06:24 -0700115func (j *Module) d8Flags(ctx android.ModuleContext, flags javaBuilderFlags) ([]string, android.Paths) {
Colin Crossffb657e2018-09-21 12:29:22 -0700116 d8Flags := j.dexCommonFlags(ctx)
117
Colin Crossc2557d12019-10-31 15:22:57 -0700118 d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
119 d8Flags = append(d8Flags, flags.classpath.FormRepeatedClassPath("--lib ")...)
Colin Crossffb657e2018-09-21 12:29:22 -0700120
Colin Cross6dab9bd2018-09-28 08:06:24 -0700121 var d8Deps android.Paths
122 d8Deps = append(d8Deps, flags.bootClasspath...)
123 d8Deps = append(d8Deps, flags.classpath...)
124
125 return d8Flags, d8Deps
Colin Crossffb657e2018-09-21 12:29:22 -0700126}
127
Colin Cross66dbc0b2017-12-28 12:23:20 -0800128func (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 Crossbafb8972018-06-06 21:46:32 +0000133 // classes added by desugaring, we artifically raise the "SDK version" "linked" by
Colin Cross66dbc0b2017-12-28 12:23:20 -0800134 // 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 Crossffb657e2018-09-21 12:29:22 -0700143 r8Flags = append(r8Flags, j.dexCommonFlags(ctx)...)
144
Colin Cross66dbc0b2017-12-28 12:23:20 -0800145 r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
146 r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
147 r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars"))
Colin Cross66dbc0b2017-12-28 12:23:20 -0800148
Colin Cross6dab9bd2018-09-28 08:06:24 -0700149 r8Deps = append(r8Deps, proguardRaiseDeps...)
150 r8Deps = append(r8Deps, flags.bootClasspath...)
151 r8Deps = append(r8Deps, flags.classpath...)
152
Colin Cross66dbc0b2017-12-28 12:23:20 -0800153 flagFiles := android.Paths{
154 android.PathForSource(ctx, "build/make/core/proguard.flags"),
155 }
156
Colin Cross3144dfc2018-01-03 15:06:47 -0800157 if j.shouldInstrumentStatic(ctx) {
158 flagFiles = append(flagFiles,
159 android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
160 }
161
Colin Cross66dbc0b2017-12-28 12:23:20 -0800162 flagFiles = append(flagFiles, j.extraProguardFlagFiles...)
163 // TODO(ccross): static android library proguard files
164
Colin Crossbd1cef52018-08-13 10:28:18 -0700165 flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, j.deviceProperties.Optimize.Proguard_flags_files)...)
166
Colin Cross66dbc0b2017-12-28 12:23:20 -0800167 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 Cross4b964c02018-10-15 16:18:06 -0700189 // 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 Cross66dbc0b2017-12-28 12:23:20 -0800191
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800192 // Don't strip out debug information for eng builds.
193 if ctx.Config().Eng() {
194 r8Flags = append(r8Flags, "--debug")
195 }
196
Colin Cross66dbc0b2017-12-28 12:23:20 -0800197 return r8Flags, r8Deps
198}
199
Colin Crossf0056cb2017-12-22 15:56:08 -0800200func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -0700201 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crossf0056cb2017-12-22 15:56:08 -0800202
Sasha Smundak2057f822019-04-16 17:16:58 -0700203 useR8 := j.deviceProperties.EffectiveOptimizeEnabled()
Colin Crossf0056cb2017-12-22 15:56:08 -0800204
Colin Crossf0056cb2017-12-22 15:56:08 -0800205 // 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 Smundakd3cf4ee2019-02-15 10:14:23 -0800209 zipFlags := "--ignore_missing_files"
David Srbeckye033cba2020-05-20 22:20:28 +0100210 if proptools.Bool(j.deviceProperties.Uncompress_dex) {
Sasha Smundakd3cf4ee2019-02-15 10:14:23 -0800211 zipFlags += " -L 0"
Colin Cross5a0dcd52018-10-05 14:20:06 -0700212 }
213
Colin Cross66dbc0b2017-12-28 12:23:20 -0800214 if useR8 {
Colin Crossc0c664c2018-05-16 16:47:21 -0700215 proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
216 j.proguardDictionary = proguardDictionary
Colin Cross66dbc0b2017-12-28 12:23:20 -0800217 r8Flags, r8Deps := j.r8Flags(ctx, flags)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400218 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 Cross66dbc0b2017-12-28 12:23:20 -0800229 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400230 Rule: rule,
Colin Crossc0c664c2018-05-16 16:47:21 -0700231 Description: "r8",
232 Output: javalibJar,
233 ImplicitOutput: proguardDictionary,
234 Input: classesJar,
235 Implicits: r8Deps,
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400236 Args: args,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800237 })
238 } else {
Colin Cross6dab9bd2018-09-28 08:06:24 -0700239 d8Flags, d8Deps := j.d8Flags(ctx, flags)
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400240 rule := d8
241 if ctx.Config().IsEnvTrue("RBE_D8") {
242 rule = d8RE
243 }
Colin Cross66dbc0b2017-12-28 12:23:20 -0800244 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400245 Rule: rule,
Colin Crossbafb8972018-06-06 21:46:32 +0000246 Description: "d8",
Colin Cross66dbc0b2017-12-28 12:23:20 -0800247 Output: javalibJar,
248 Input: classesJar,
Colin Cross6dab9bd2018-09-28 08:06:24 -0700249 Implicits: d8Deps,
Colin Cross66dbc0b2017-12-28 12:23:20 -0800250 Args: map[string]string{
Colin Cross5a0dcd52018-10-05 14:20:06 -0700251 "d8Flags": strings.Join(d8Flags, " "),
252 "zipFlags": zipFlags,
253 "outDir": outDir.String(),
Colin Cross66dbc0b2017-12-28 12:23:20 -0800254 },
255 })
Colin Crossf0056cb2017-12-22 15:56:08 -0800256 }
David Srbeckye033cba2020-05-20 22:20:28 +0100257 if proptools.Bool(j.deviceProperties.Uncompress_dex) {
Nicolas Geoffray65fd8ba2019-01-21 23:20:23 +0000258 alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", jarName)
259 TransformZipAlign(ctx, alignedJavalibJar, javalibJar)
260 javalibJar = alignedJavalibJar
261 }
Colin Crossf0056cb2017-12-22 15:56:08 -0800262
Colin Crossf0056cb2017-12-22 15:56:08 -0800263 return javalibJar
264}