blob: fa5c2eea2f2b3b1be029f1525f813098ca9a9ada [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"
21
22 "android/soong/android"
23)
24
25var desugar = pctx.AndroidStaticRule("desugar",
26 blueprint.RuleParams{
27 Command: `rm -rf $dumpDir && mkdir -p $dumpDir && ` +
28 `${config.JavaCmd} ` +
29 `-Djdk.internal.lambda.dumpProxyClasses=$$(cd $dumpDir && pwd) ` +
30 `$javaFlags ` +
31 `-jar ${config.DesugarJar} $classpathFlags $desugarFlags ` +
32 `-i $in -o $out`,
33 CommandDeps: []string{"${config.DesugarJar}", "${config.JavaCmd}"},
34 },
35 "javaFlags", "classpathFlags", "desugarFlags", "dumpDir")
36
37func (j *Module) desugar(ctx android.ModuleContext, flags javaBuilderFlags,
38 classesJar android.Path, jarName string) android.Path {
39
40 desugarFlags := []string{
41 "--min_sdk_version " + j.minSdkVersionNumber(ctx),
42 "--desugar_try_with_resources_if_needed=false",
43 "--allow_empty_bootclasspath",
44 }
45
46 if inList("--core-library", j.deviceProperties.Dxflags) {
47 desugarFlags = append(desugarFlags, "--core_library")
48 }
49
50 desugarJar := android.PathForModuleOut(ctx, "desugar", jarName)
51 dumpDir := android.PathForModuleOut(ctx, "desugar", "classes")
52
53 javaFlags := ""
54 if ctx.Config().UseOpenJDK9() {
55 javaFlags = "--add-opens java.base/java.lang.invoke=ALL-UNNAMED"
56 }
57
58 var classpathFlags []string
59 classpathFlags = append(classpathFlags, flags.bootClasspath.FormDesugarClasspath("--bootclasspath_entry")...)
60 classpathFlags = append(classpathFlags, flags.classpath.FormDesugarClasspath("--classpath_entry")...)
61
62 var deps android.Paths
63 deps = append(deps, flags.bootClasspath...)
64 deps = append(deps, flags.classpath...)
65
66 ctx.Build(pctx, android.BuildParams{
67 Rule: desugar,
68 Description: "desugar",
69 Output: desugarJar,
70 Input: classesJar,
71 Implicits: deps,
72 Args: map[string]string{
73 "dumpDir": dumpDir.String(),
74 "javaFlags": javaFlags,
75 "classpathFlags": strings.Join(classpathFlags, " "),
76 "desugarFlags": strings.Join(desugarFlags, " "),
77 },
78 })
79
80 return desugarJar
81}
82
83var dx = pctx.AndroidStaticRule("dx",
84 blueprint.RuleParams{
85 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
86 `${config.DxCmd} --dex --output=$outDir $dxFlags $in && ` +
87 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
88 `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`,
89 CommandDeps: []string{
90 "${config.DxCmd}",
91 "${config.SoongZipCmd}",
92 "${config.MergeZipsCmd}",
93 },
94 },
95 "outDir", "dxFlags")
96
97var d8 = pctx.AndroidStaticRule("d8",
98 blueprint.RuleParams{
99 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
100 `${config.D8Cmd} --output $outDir $dxFlags $in && ` +
101 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
102 `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`,
103 CommandDeps: []string{
104 "${config.D8Cmd}",
105 "${config.SoongZipCmd}",
106 "${config.MergeZipsCmd}",
107 },
108 },
109 "outDir", "dxFlags")
110
111func (j *Module) dxFlags(ctx android.ModuleContext, fullD8 bool) []string {
112 flags := j.deviceProperties.Dxflags
113 if fullD8 {
114 // Translate all the DX flags to D8 ones until all the build files have been migrated
115 // to D8 flags. See: b/69377755
116 flags = android.RemoveListFromList(flags,
117 []string{"--core-library", "--dex", "--multi-dex"})
118 }
119
120 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
121 if fullD8 {
122 flags = append(flags, "--debug")
123 } else {
124 flags = append(flags, "--no-optimize")
125 }
126 }
127
128 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
129 flags = append(flags,
130 "--debug",
131 "--verbose")
132 if !fullD8 {
133 flags = append(flags,
134 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
135 "--dump-width=1000")
136 }
137 }
138
139 if fullD8 {
140 flags = append(flags, "--min-api "+j.minSdkVersionNumber(ctx))
141 } else {
142 flags = append(flags, "--min-sdk-version="+j.minSdkVersionNumber(ctx))
143 }
144 return flags
145}
146
147func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
148 classesJar android.Path, jarName string) android.Path {
149
150 fullD8 := ctx.Config().UseD8Desugar()
151
152 if !fullD8 {
153 classesJar = j.desugar(ctx, flags, classesJar, jarName)
154 }
155
156 dxFlags := j.dxFlags(ctx, fullD8)
157
158 // Compile classes.jar into classes.dex and then javalib.jar
159 javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
160 outDir := android.PathForModuleOut(ctx, "dex")
161
162 rule := dx
163 desc := "dx"
164 if fullD8 {
165 rule = d8
166 desc = "d8"
167 }
168 ctx.Build(pctx, android.BuildParams{
169 Rule: rule,
170 Description: desc,
171 Output: javalibJar,
172 Input: classesJar,
173 Args: map[string]string{
174 "dxFlags": strings.Join(dxFlags, " "),
175 "outDir": outDir.String(),
176 },
177 })
178
179 j.dexJarFile = javalibJar
180 return javalibJar
181}