Bring java support closer to current version of make

Make the javac arguments match what is used by make, and export them
back to make.  A future change will switch make to use the the
exported ones.

This makes a dx.jar compiled with soong have identical class files
as one compiled with make.

Test: manual
Change-Id: Ia5196f1f42bc564e99de22e32e72fd2930e9fbae
This commit is contained in:
Colin Cross 2017-08-08 13:17:59 -07:00
parent 74d73e2bfb
commit 6416271a1f
5 changed files with 99 additions and 29 deletions

View File

@ -75,6 +75,22 @@ func (p AndroidPackageContext) SourcePathVariable(name, path string) blueprint.V
})
}
// SourcePathVariableWithEnvOverride returns a Variable whose value is the source directory
// appended with the supplied path, or the value of the given environment variable if it is set.
// The environment variable is not required to point to a path inside the source tree.
// It may only be called during a Go package's initialization - either from the init() function or
// as part of a package-scoped variable's initialization.
func (p AndroidPackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable {
return p.VariableFunc(name, func(config interface{}) (string, error) {
ctx := &configErrorWrapper{p, config.(Config), []error{}}
p := safePathForSource(ctx, path)
if len(ctx.errors) > 0 {
return "", ctx.errors[0]
}
return config.(Config).GetenvWithDefault(env, p.String()), nil
})
}
// HostBinVariable returns a Variable whose value is the path to a host tool
// in the bin directory for host targets. It may only be called during a Go
// package's initialization - either from the init() function or as part of a

View File

@ -19,13 +19,11 @@ package java
// functions.
import (
"path/filepath"
"strings"
"android/soong/android"
"github.com/google/blueprint"
_ "github.com/google/blueprint/bootstrap"
)
var (
@ -39,36 +37,37 @@ var (
// read from directly using @<listfile>)
javac = pctx.AndroidGomaStaticRule("javac",
blueprint.RuleParams{
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
`${JavacWrapper}$javacCmd ` +
`-encoding UTF-8 $javacFlags $bootClasspath $classpath ` +
`-extdirs "" -d $outDir @$out.rsp || ( rm -rf "$outDir"; exit 41 ) && ` +
Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` +
`${config.JavacWrapper}${config.JavacCmd} ${config.CommonJdkFlags} ` +
`$javacFlags $bootClasspath $classpath ` +
`-source $javaVersion -target $javaVersion ` +
`-d $outDir -s $annoDir @$out.rsp || ( rm -rf "$outDir"; exit 41 ) && ` +
`find $outDir -name "*.class" > $out`,
Rspfile: "$out.rsp",
RspfileContent: "$in",
},
"javacCmd", "javacFlags", "bootClasspath", "classpath", "outDir")
"javacFlags", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
jar = pctx.AndroidStaticRule("jar",
blueprint.RuleParams{
Command: `$jarCmd -o $out $jarArgs`,
CommandDeps: []string{"$jarCmd"},
Command: `${config.SoongZipCmd} -o $out -d $jarArgs`,
CommandDeps: []string{"${config.SoongZipCmd}"},
},
"jarCmd", "jarArgs")
dx = pctx.AndroidStaticRule("dx",
blueprint.RuleParams{
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
`$dxCmd --dex --output=$outDir $dxFlags $in || ( rm -rf "$outDir"; exit 41 ) && ` +
`find "$outDir" -name "classes*.dex" > $out`,
CommandDeps: []string{"$dxCmd"},
`${config.DxCmd} --dex --output=$outDir $dxFlags $in || ( rm -rf "$outDir"; exit 41 ) && ` +
`find "$outDir" -name "classes*.dex" | sort > $out`,
CommandDeps: []string{"${config.DxCmd}"},
},
"outDir", "dxFlags")
jarjar = pctx.AndroidStaticRule("jarjar",
blueprint.RuleParams{
Command: "java -jar $jarjarCmd process $rulesFile $in $out",
CommandDeps: []string{"$jarjarCmd", "$rulesFile"},
Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
},
"rulesFile")
@ -83,19 +82,7 @@ var (
)
func init() {
pctx.Import("github.com/google/blueprint/bootstrap")
pctx.StaticVariable("commonJdkFlags", "-source 1.7 -target 1.7 -Xmaxerrs 9999999")
pctx.StaticVariable("javacCmd", "javac -J-Xmx1024M $commonJdkFlags")
pctx.StaticVariable("jarCmd", filepath.Join("${bootstrap.ToolDir}", "soong_zip"))
pctx.HostBinToolVariable("dxCmd", "dx")
pctx.HostJavaToolVariable("jarjarCmd", "jarjar.jar")
pctx.VariableFunc("JavacWrapper", func(config interface{}) (string, error) {
if override := config.(android.Config).Getenv("JAVAC_WRAPPER"); override != "" {
return override + " ", nil
}
return "", nil
})
pctx.Import("android/soong/java/config")
}
type javaBuilderFlags struct {
@ -104,6 +91,7 @@ type javaBuilderFlags struct {
bootClasspath string
classpath string
aidlFlags string
javaVersion string
}
type jarSpec struct {
@ -118,6 +106,7 @@ func TransformJavaToClasses(ctx android.ModuleContext, srcFiles android.Paths, s
flags javaBuilderFlags, deps android.Paths) jarSpec {
classDir := android.PathForModuleOut(ctx, "classes")
annoDir := android.PathForModuleOut(ctx, "anno")
classFileList := android.PathForModuleOut(ctx, "classes.list")
javacFlags := flags.javacFlags + android.JoinWithPrefix(srcFileLists.Strings(), "@")
@ -135,6 +124,8 @@ func TransformJavaToClasses(ctx android.ModuleContext, srcFiles android.Paths, s
"bootClasspath": flags.bootClasspath,
"classpath": flags.classpath,
"outDir": classDir.String(),
"annoDir": annoDir.String(),
"javaVersion": flags.javaVersion,
},
})

View File

@ -14,10 +14,53 @@
package config
import "android/soong/android"
import (
"path/filepath"
"strings"
_ "github.com/google/blueprint/bootstrap"
"android/soong/android"
)
var (
pctx = android.NewPackageContext("android/soong/java/config")
DefaultLibraries = []string{"core-oj", "core-libart", "ext", "framework", "okhttp"}
)
var pctx = android.NewPackageContext("android/soong/java/config")
func init() {
pctx.Import("github.com/google/blueprint/bootstrap")
pctx.StaticVariable("CommonJdkFlags", strings.Join([]string{
`-J-Xmx2048M`,
`-Xmaxerrs 9999999`,
`-encoding UTF-8`,
`-sourcepath ""`,
`-g`,
}, " "))
pctx.StaticVariable("DefaultJavaVersion", "1.8")
pctx.VariableConfigMethod("hostPrebuiltTag", android.Config.PrebuiltOS)
pctx.SourcePathVariableWithEnvOverride("JavaHome",
"prebuilts/jdk/jdk8/${hostPrebuiltTag}", "OVERRIDE_ANDROID_JAVA_HOME")
pctx.SourcePathVariable("JavaToolchain", "${JavaHome}/bin")
pctx.SourcePathVariableWithEnvOverride("JavacCmd",
"${JavaToolchain}/javac", "ALTERNATE_JAVAC")
pctx.SourcePathVariable("JavaCmd", "${JavaToolchain}/java")
pctx.SourcePathVariable("JarCmd", "${JavaToolchain}/jar")
pctx.SourcePathVariable("JavadocCmd", "${JavaToolchain}/javadoc")
pctx.StaticVariable("SoongZipCmd", filepath.Join("${bootstrap.ToolDir}", "soong_zip"))
pctx.HostBinToolVariable("DxCmd", "dx")
pctx.HostJavaToolVariable("JarjarCmd", "jarjar.jar")
pctx.VariableFunc("JavacWrapper", func(config interface{}) (string, error) {
if override := config.(android.Config).Getenv("JAVAC_WRAPPER"); override != "" {
return override + " ", nil
}
return "", nil
})
}

View File

@ -26,4 +26,14 @@ func init() {
func makeVarsProvider(ctx android.MakeVarsContext) {
ctx.Strict("TARGET_DEFAULT_JAVA_LIBRARIES", strings.Join(DefaultLibraries, " "))
ctx.Strict("DEFAULT_JAVA_LANGUAGE_VERSION", "${DefaultJavaVersion}")
ctx.Strict("ANDROID_JAVA_HOME", "${JavaHome}")
ctx.Strict("ANDROID_JAVA_TOOLCHAIN", "${JavaToolchain}")
ctx.Strict("JAVA", "${JavaCmd}")
ctx.Strict("JAVAC", "${JavacCmd}")
ctx.Strict("JAR", "${JarCmd}")
ctx.Strict("JAVADOC", "${JavadocCmd}")
ctx.Strict("COMMON_JDK_FLAGS", "${CommonJdkFlags}")
}

View File

@ -92,6 +92,9 @@ type CompilerProperties struct {
// if not blank, run jarjar using the specified rules file
Jarjar_rules *string
// If not blank, set the java version passed to javac as -source and -target
Java_version *string
}
type CompilerDeviceProperties struct {
@ -285,6 +288,13 @@ func (j *Module) compile(ctx android.ModuleContext) {
var flags javaBuilderFlags
javacFlags := j.properties.Javacflags
if j.properties.Java_version != nil {
flags.javaVersion = *j.properties.Java_version
} else {
flags.javaVersion = "${config.DefaultJavaVersion}"
}
if len(javacFlags) > 0 {
ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
flags.javacFlags = "$javacFlags"