From 7576916efcc5e73d3b5e6437187697a451dd43f0 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 6 Jun 2018 21:42:39 +0000 Subject: [PATCH] Revert "Remove desugar support" This reverts commit 000280bb0e13902b2d19fd102ecf68d0dbbabfba. Reason for revert: broke art veridex build Change-Id: I9f7d7cdbe8e34671614687ac0b34c0338ef240e7 --- android/config.go | 4 ++ java/builder.go | 6 +- java/config/config.go | 13 +++++ java/config/makevars.go | 11 +++- java/dex.go | 120 ++++++++++++++++++++++++++++++++++++---- 5 files changed, 138 insertions(+), 16 deletions(-) diff --git a/android/config.go b/android/config.go index f5a5e8eb6..bc3d4f5e6 100644 --- a/android/config.go +++ b/android/config.go @@ -627,6 +627,10 @@ func (c *config) Android64() bool { return false } +func (c *config) UseD8Desugar() bool { + return !c.IsEnvFalse("USE_D8_DESUGAR") +} + func (c *config) UseGoma() bool { return Bool(c.productVariables.UseGoma) } diff --git a/java/builder.go b/java/builder.go index d338623dc..d36e0dc0f 100644 --- a/java/builder.go +++ b/java/builder.go @@ -243,7 +243,7 @@ func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android. // ensure java does not fall back to the default bootclasspath. bootClasspath = `--bootclasspath ""` } else { - bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath"), " ") + bootClasspath = strings.Join(flags.bootClasspath.FormDesugarClasspath("--bootclasspath"), " ") } ctx.Build(pctx, android.BuildParams{ @@ -256,7 +256,7 @@ func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android. "javacFlags": flags.javacFlags, "bootClasspath": bootClasspath, "srcJars": strings.Join(srcJars.Strings(), " "), - "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath"), " "), + "classpath": strings.Join(flags.classpath.FormDesugarClasspath("--classpath"), " "), "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(), "javaVersion": flags.javaVersion, }, @@ -419,7 +419,7 @@ func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) s } } -func (x *classpath) FormTurbineClasspath(optName string) []string { +func (x *classpath) FormDesugarClasspath(optName string) []string { if x == nil || *x == nil { return nil } diff --git a/java/config/config.go b/java/config/config.go index c6555f196..fe57c50d9 100644 --- a/java/config/config.go +++ b/java/config/config.go @@ -93,6 +93,18 @@ func init() { pctx.HostBinToolVariable("Zip2ZipCmd", "zip2zip") pctx.HostBinToolVariable("ZipSyncCmd", "zipsync") pctx.HostBinToolVariable("ApiCheckCmd", "apicheck") + pctx.VariableFunc("DxCmd", func(ctx android.PackageVarContext) string { + config := ctx.Config() + if config.IsEnvFalse("USE_D8") { + if config.UnbundledBuild() || config.IsPdkBuild() { + return "prebuilts/build-tools/common/bin/dx" + } else { + return pctx.HostBinToolPath(ctx, "dx").String() + } + } else { + return pctx.HostBinToolPath(ctx, "d8-compat-dx").String() + } + }) pctx.HostBinToolVariable("D8Cmd", "d8") pctx.HostBinToolVariable("R8Cmd", "r8-compat-proguard") @@ -106,6 +118,7 @@ func init() { }) pctx.HostJavaToolVariable("JarjarCmd", "jarjar.jar") + pctx.HostJavaToolVariable("DesugarJar", "desugar.jar") pctx.HostJavaToolVariable("JsilverJar", "jsilver.jar") pctx.HostJavaToolVariable("DoclavaJar", "doclava.jar") pctx.HostJavaToolVariable("MetalavaJar", "metalava.jar") diff --git a/java/config/makevars.go b/java/config/makevars.go index 4dffa0246..27c7daaa5 100644 --- a/java/config/makevars.go +++ b/java/config/makevars.go @@ -46,8 +46,15 @@ func makeVarsProvider(ctx android.MakeVarsContext) { ctx.Strict("JAVADOC", "${JavadocCmd}") ctx.Strict("COMMON_JDK_FLAGS", "${CommonJdkFlags}") - ctx.Strict("DX", "${D8Cmd}") - ctx.Strict("DX_COMMAND", "${D8Cmd} -JXms16M -JXmx2048M") + if ctx.Config().UseD8Desugar() { + ctx.Strict("DX", "${D8Cmd}") + ctx.Strict("DX_COMMAND", "${D8Cmd} -JXms16M -JXmx2048M") + ctx.Strict("USE_D8_DESUGAR", "true") + } else { + ctx.Strict("DX", "${DxCmd}") + ctx.Strict("DX_COMMAND", "${DxCmd} -JXms16M -JXmx2048M") + ctx.Strict("USE_D8_DESUGAR", "false") + } ctx.Strict("R8_COMPAT_PROGUARD", "${R8Cmd}") ctx.Strict("TURBINE", "${TurbineJar}") diff --git a/java/dex.go b/java/dex.go index f729badea..642dee4a3 100644 --- a/java/dex.go +++ b/java/dex.go @@ -22,6 +22,78 @@ import ( "android/soong/android" ) +var desugar = pctx.AndroidStaticRule("desugar", + blueprint.RuleParams{ + Command: `rm -rf $dumpDir && mkdir -p $dumpDir && ` + + `${config.JavaCmd} ` + + `-Djdk.internal.lambda.dumpProxyClasses=$$(cd $dumpDir && pwd) ` + + `$javaFlags ` + + `-jar ${config.DesugarJar} $classpathFlags $desugarFlags ` + + `-i $in -o $out`, + CommandDeps: []string{"${config.DesugarJar}", "${config.JavaCmd}"}, + }, + "javaFlags", "classpathFlags", "desugarFlags", "dumpDir") + +func (j *Module) desugar(ctx android.ModuleContext, flags javaBuilderFlags, + classesJar android.Path, jarName string) android.Path { + + desugarFlags := []string{ + "--min_sdk_version " + j.minSdkVersionNumber(ctx), + "--desugar_try_with_resources_if_needed=false", + "--allow_empty_bootclasspath", + } + + if inList("--core-library", j.deviceProperties.Dxflags) { + desugarFlags = append(desugarFlags, "--core_library") + } + + desugarJar := android.PathForModuleOut(ctx, "desugar", jarName) + dumpDir := android.PathForModuleOut(ctx, "desugar", "classes") + + javaFlags := "" + if ctx.Config().UseOpenJDK9() { + javaFlags = "--add-opens java.base/java.lang.invoke=ALL-UNNAMED" + } + + var classpathFlags []string + classpathFlags = append(classpathFlags, flags.bootClasspath.FormDesugarClasspath("--bootclasspath_entry")...) + classpathFlags = append(classpathFlags, flags.classpath.FormDesugarClasspath("--classpath_entry")...) + + var deps android.Paths + deps = append(deps, flags.bootClasspath...) + deps = append(deps, flags.classpath...) + + ctx.Build(pctx, android.BuildParams{ + Rule: desugar, + Description: "desugar", + Output: desugarJar, + Input: classesJar, + Implicits: deps, + Args: map[string]string{ + "dumpDir": dumpDir.String(), + "javaFlags": javaFlags, + "classpathFlags": strings.Join(classpathFlags, " "), + "desugarFlags": strings.Join(desugarFlags, " "), + }, + }) + + return desugarJar +} + +var dx = pctx.AndroidStaticRule("dx", + blueprint.RuleParams{ + Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + + `${config.DxCmd} --dex --output=$outDir $dxFlags $in && ` + + `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` + + `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`, + CommandDeps: []string{ + "${config.DxCmd}", + "${config.SoongZipCmd}", + "${config.MergeZipsCmd}", + }, + }, + "outDir", "dxFlags") + var d8 = pctx.AndroidStaticRule("d8", blueprint.RuleParams{ Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + @@ -53,24 +125,39 @@ var r8 = pctx.AndroidStaticRule("r8", }, "outDir", "outDict", "dxFlags", "r8Flags") -func (j *Module) dxFlags(ctx android.ModuleContext) []string { +func (j *Module) dxFlags(ctx android.ModuleContext, fullD8 bool) []string { flags := j.deviceProperties.Dxflags - // Translate all the DX flags to D8 ones until all the build files have been migrated - // to D8 flags. See: b/69377755 - flags = android.RemoveListFromList(flags, - []string{"--core-library", "--dex", "--multi-dex"}) + if fullD8 { + // Translate all the DX flags to D8 ones until all the build files have been migrated + // to D8 flags. See: b/69377755 + flags = android.RemoveListFromList(flags, + []string{"--core-library", "--dex", "--multi-dex"}) + } if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" { - flags = append(flags, "--debug") + if fullD8 { + flags = append(flags, "--debug") + } else { + flags = append(flags, "--no-optimize") + } } if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" { flags = append(flags, "--debug", "--verbose") + if !fullD8 { + flags = append(flags, + "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(), + "--dump-width=1000") + } } - flags = append(flags, "--min-api "+j.minSdkVersionNumber(ctx)) + if fullD8 { + flags = append(flags, "--min-api "+j.minSdkVersionNumber(ctx)) + } else { + flags = append(flags, "--min-sdk-version="+j.minSdkVersionNumber(ctx)) + } return flags } @@ -79,7 +166,7 @@ func (j *Module) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8F // When an app contains references to APIs that are not in the SDK specified by // its LOCAL_SDK_VERSION for example added by support library or by runtime - // classes added by desugaring, we artifically raise the "SDK version" "linked" by + // classes added by desugar, we artifically raise the "SDK version" "linked" by // ProGuard, to // - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version. // - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version. @@ -136,8 +223,13 @@ func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags, classesJar android.Path, jarName string) android.Path { useR8 := Bool(j.deviceProperties.Optimize.Enabled) + fullD8 := useR8 || ctx.Config().UseD8Desugar() - dxFlags := j.dxFlags(ctx) + if !fullD8 { + classesJar = j.desugar(ctx, flags, classesJar, jarName) + } + + dxFlags := j.dxFlags(ctx, fullD8) // Compile classes.jar into classes.dex and then javalib.jar javalibJar := android.PathForModuleOut(ctx, "dex", jarName) @@ -164,9 +256,15 @@ func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags, }, }) } else { + rule := dx + desc := "dx" + if fullD8 { + rule = d8 + desc = "d8" + } ctx.Build(pctx, android.BuildParams{ - Rule: d8, - Description: "d8", + Rule: rule, + Description: desc, Output: javalibJar, Input: classesJar, Args: map[string]string{