Support Turbine in Soong.

If sdk jars(android_stubs_current, etc) are compiled using soong java
modules, we have to filter them when running Java build with Turbine.

TODO: provide more unit-tests.

Test: m clean && m -j32; go test java_test

Change-Id: Iad7c241b0e8b0ca760950733f513124b56c84564
This commit is contained in:
Nan Zhang 2017-10-19 13:06:22 -07:00
parent 9e07394fd0
commit ed19fc3e2c
8 changed files with 317 additions and 177 deletions

View file

@ -95,6 +95,15 @@ func (m TestingModule) Rule(rule string) ModuleBuildParams {
panic(fmt.Errorf("couldn't find rule %q", rule))
}
func (m TestingModule) Description(desc string) ModuleBuildParams {
for _, p := range m.module.BuildParamsForTests() {
if p.Description == desc {
return p
}
}
panic(fmt.Errorf("couldn't find description %q", desc))
}
func (m TestingModule) Output(file string) ModuleBuildParams {
for _, p := range m.module.BuildParamsForTests() {
outputs := append(WritablePaths(nil), p.Outputs...)

View file

@ -27,7 +27,7 @@ import (
func (library *Library) AndroidMk() android.AndroidMkData {
return android.AndroidMkData{
Class: "JAVA_LIBRARIES",
OutputFile: android.OptionalPathForPath(library.classpathFile),
OutputFile: android.OptionalPathForPath(library.implementationJarFile),
Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
Extra: []android.AndroidMkExtraFunc{
func(w io.Writer, outputFile android.Path) {
@ -41,6 +41,7 @@ func (library *Library) AndroidMk() android.AndroidMkData {
}
}
fmt.Fprintln(w, "LOCAL_SDK_VERSION :=", library.deviceProperties.Sdk_version)
fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", library.headerJarFile.String())
},
},
Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
@ -51,13 +52,14 @@ func (library *Library) AndroidMk() android.AndroidMkData {
fmt.Fprintln(w, "LOCAL_MODULE := "+name+"-hostdex")
fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
fmt.Fprintln(w, "LOCAL_MODULE_CLASS := JAVA_LIBRARIES")
fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", library.classpathFile.String())
fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", library.implementationJarFile.String())
if library.properties.Installable != nil && *library.properties.Installable == false {
fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
}
if library.dexJarFile != nil {
fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", library.dexJarFile.String())
}
fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", library.implementationJarFile.String())
fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " "))
fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
}
@ -73,6 +75,7 @@ func (prebuilt *Import) AndroidMk() android.AndroidMkData {
Extra: []android.AndroidMkExtraFunc{
func(w io.Writer, outputFile android.Path) {
fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", prebuilt.combinedClasspathFile.String())
},
},
}
@ -81,7 +84,7 @@ func (prebuilt *Import) AndroidMk() android.AndroidMkData {
func (binary *Binary) AndroidMk() android.AndroidMkData {
return android.AndroidMkData{
Class: "JAVA_LIBRARIES",
OutputFile: android.OptionalPathForPath(binary.classpathFile),
OutputFile: android.OptionalPathForPath(binary.implementationJarFile),
Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
android.WriteAndroidMkData(w, data)

View file

@ -87,6 +87,22 @@ var (
},
"javacFlags", "sourcepath", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
turbine = pctx.AndroidStaticRule("turbine",
blueprint.RuleParams{
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
`${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
`--temp_dir "$outDir" --sources @$out.rsp $sourcepath ` +
`--javacopts ${config.CommonJdkFlags} ` +
`$javacFlags -source $javaVersion -target $javaVersion $bootClasspath $classpath && ` +
`${config.Ziptime} $out.tmp && ` +
`(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
CommandDeps: []string{"${config.TurbineJar}", "${config.JavaCmd}", "${config.Ziptime}"},
Rspfile: "$out.rsp",
RspfileContent: "$in",
Restat: true,
},
"javacFlags", "sourcepath", "bootClasspath", "classpath", "outDir", "javaVersion")
jar = pctx.AndroidStaticRule("jar",
blueprint.RuleParams{
Command: `${config.SoongZipCmd} -jar -o $out $jarArgs`,
@ -109,7 +125,7 @@ var (
`$javaFlags ` +
`-jar ${config.DesugarJar} $classpathFlags $desugarFlags ` +
`-i $in -o $out`,
CommandDeps: []string{"${config.DesugarJar}"},
CommandDeps: []string{"${config.DesugarJar}", "${config.JavaCmd}"},
},
"javaFlags", "classpathFlags", "desugarFlags", "dumpDir")
@ -171,7 +187,7 @@ func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.Writ
Output: outputFile,
Inputs: inputs,
Args: map[string]string{
"classpath": flags.kotlincClasspath.JavaClasspath(),
"classpath": flags.kotlincClasspath.FormJavaClassPath("--classpath"),
"kotlincFlags": flags.kotlincFlags,
"outDir": classDir.String(),
"javaVersion": flags.javaVersion,
@ -188,8 +204,7 @@ func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.Writab
}
func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
srcFiles android.Paths, srcJars classpath,
flags javaBuilderFlags) {
srcFiles android.Paths, srcJars classpath, flags javaBuilderFlags) {
if config.ErrorProneJar == "" {
ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
@ -199,6 +214,45 @@ func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
"errorprone", "errorprone", errorprone)
}
func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
srcFiles android.Paths, srcJars classpath, flags javaBuilderFlags) {
var deps android.Paths
deps = append(deps, srcJars...)
deps = append(deps, flags.bootClasspath...)
deps = append(deps, flags.classpath...)
var bootClasspath string
if len(flags.bootClasspath) == 0 && ctx.Device() {
// explicitly specify -bootclasspath "" if the bootclasspath is empty to
// ensure java does not fall back to the default bootclasspath.
bootClasspath = `--bootclasspath ""`
} else {
bootClasspath = flags.bootClasspath.FormJavaClassPath("--bootclasspath")
}
var sourcepath string
if len(srcJars) > 0 {
sourcepath = "--sourcepath_jars" + " " + strings.Join(srcJars.Strings(), " ")
} else {
sourcepath = ""
}
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: turbine,
Description: "turbine",
Output: outputFile,
Inputs: srcFiles,
Implicits: deps,
Args: map[string]string{
"javacFlags": flags.javacFlags,
"bootClasspath": bootClasspath,
"sourcepath": sourcepath,
"classpath": flags.classpath.FormJavaClassPath("--classpath"),
"outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
"javaVersion": flags.javaVersion,
},
})
}
// transformJavaToClasses takes source files and converts them to a jar containing .class files.
// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
// sources. flags contains various command line flags to be passed to the compiler.
@ -218,10 +272,16 @@ func transformJavaToClasses(ctx android.ModuleContext, outputFile android.Writab
var bootClasspath string
if flags.javaVersion == "1.9" {
deps = append(deps, flags.systemModules...)
bootClasspath = flags.systemModules.JavaSystemModules(ctx.Device())
bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
} else {
deps = append(deps, flags.bootClasspath...)
bootClasspath = flags.bootClasspath.JavaBootClasspath(ctx.Device())
if len(flags.bootClasspath) == 0 && ctx.Device() {
// explicitly specify -bootclasspath "" if the bootclasspath is empty to
// ensure java does not fall back to the default bootclasspath.
bootClasspath = `-bootclasspath ""`
} else {
bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
}
}
deps = append(deps, flags.classpath...)
@ -235,11 +295,13 @@ func transformJavaToClasses(ctx android.ModuleContext, outputFile android.Writab
Args: map[string]string{
"javacFlags": flags.javacFlags,
"bootClasspath": bootClasspath,
"sourcepath": srcJars.JavaSourcepath(),
"classpath": flags.classpath.JavaClasspath(),
"outDir": android.PathForModuleOut(ctx, intermediatesDir, "classes").String(),
"annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
"javaVersion": flags.javaVersion,
// Returns a -sourcepath argument in the form javac expects. If the list is empty returns
// -sourcepath "" to ensure javac does not fall back to searching the classpath for sources.
"sourcepath": srcJars.FormJavaClassPath("-sourcepath"),
"classpath": flags.classpath.FormJavaClassPath("-classpath"),
"outDir": android.PathForModuleOut(ctx, intermediatesDir, "classes").String(),
"annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
"javaVersion": flags.javaVersion,
},
})
}
@ -258,24 +320,30 @@ func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.Writa
})
}
func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
jars android.Paths, manifest android.OptionalPath, stripDirs bool) {
func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
jars android.Paths, manifest android.OptionalPath, stripDirs bool, dirsToStrip []string) {
var deps android.Paths
var jarArgs []string
if manifest.Valid() {
jarArgs = append(jarArgs, "-m "+manifest.String())
jarArgs = append(jarArgs, "-m ", manifest.String())
deps = append(deps, manifest.Path())
}
if dirsToStrip != nil {
for _, dir := range dirsToStrip {
jarArgs = append(jarArgs, "-stripDir ", dir)
}
}
if stripDirs {
jarArgs = append(jarArgs, "-D")
}
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: combineJar,
Description: "combine jars",
Description: desc,
Output: outputFile,
Inputs: jars,
Implicits: deps,
@ -296,8 +364,8 @@ func TransformDesugar(ctx android.ModuleContext, outputFile android.WritablePath
}
var desugarFlags []string
desugarFlags = append(desugarFlags, flags.bootClasspath.DesugarBootClasspath()...)
desugarFlags = append(desugarFlags, flags.classpath.DesugarClasspath()...)
desugarFlags = append(desugarFlags, flags.bootClasspath.FormDesugarClasspath("--bootclasspath_entry")...)
desugarFlags = append(desugarFlags, flags.classpath.FormDesugarClasspath("--classpath_entry")...)
var deps android.Paths
deps = append(deps, flags.bootClasspath...)
@ -353,42 +421,9 @@ func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
type classpath []android.Path
// Returns a -sourcepath argument in the form javac expects. If the list is empty returns
// -sourcepath "" to ensure javac does not fall back to searching the classpath for sources.
func (x *classpath) JavaSourcepath() string {
func (x *classpath) FormJavaClassPath(optName string) string {
if len(*x) > 0 {
return "-sourcepath " + strings.Join(x.Strings(), ":")
} else {
return `-sourcepath ""`
}
}
// Returns a -classpath argument in the form java or javac expects
func (x *classpath) JavaClasspath() string {
if len(*x) > 0 {
return "-classpath " + strings.Join(x.Strings(), ":")
} else {
return ""
}
}
// Returns a -processorpath argument in the form java or javac expects
func (x *classpath) JavaProcessorpath() string {
if len(*x) > 0 {
return "-processorpath " + strings.Join(x.Strings(), ":")
} else {
return ""
}
}
// Returns a -bootclasspath argument in the form java or javac expects. If forceEmpty is true,
// returns -bootclasspath "" if the bootclasspath is empty to ensure javac does not fall back to the
// default bootclasspath.
func (x *classpath) JavaBootClasspath(forceEmpty bool) string {
if len(*x) > 0 {
return "-bootclasspath " + strings.Join(x.Strings(), ":")
} else if forceEmpty {
return `-bootclasspath ""`
return optName + " " + strings.Join(x.Strings(), ":")
} else {
return ""
}
@ -397,37 +432,25 @@ func (x *classpath) JavaBootClasspath(forceEmpty bool) string {
// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
// returns --system=none if the list is empty to ensure javac does not fall back to the default
// system modules.
func (x *classpath) JavaSystemModules(forceEmpty bool) string {
func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
if len(*x) > 1 {
panic("more than one system module")
} else if len(*x) == 1 {
return "--system=" + strings.TrimSuffix((*x)[0].String(), "lib/modules")
return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
} else if forceEmpty {
return "--system=none"
return optName + "none"
} else {
return ""
}
}
func (x *classpath) DesugarBootClasspath() []string {
func (x *classpath) FormDesugarClasspath(optName string) []string {
if x == nil || *x == nil {
return nil
}
flags := make([]string, len(*x))
for i, v := range *x {
flags[i] = "--bootclasspath_entry " + v.String()
}
return flags
}
func (x *classpath) DesugarClasspath() []string {
if x == nil || *x == nil {
return nil
}
flags := make([]string, len(*x))
for i, v := range *x {
flags[i] = "--classpath_entry " + v.String()
flags[i] = optName + " " + v.String()
}
return flags

View file

@ -73,6 +73,7 @@ func init() {
pctx.SourcePathVariable("JlinkCmd", "${JavaToolchain}/jlink")
pctx.SourcePathVariable("JmodCmd", "${JavaToolchain}/jmod")
pctx.SourcePathVariable("JrtFsJar", "${JavaHome}/lib/jrt-fs.jar")
pctx.SourcePathVariable("Ziptime", "prebuilts/build-tools/${hostPrebuiltTag}/bin/ziptime")
pctx.SourcePathVariable("JarArgsCmd", "build/soong/scripts/jar-args.sh")
pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
@ -95,6 +96,7 @@ func init() {
pctx.HostJavaToolVariable("JarjarCmd", "jarjar.jar")
pctx.HostJavaToolVariable("DesugarJar", "desugar.jar")
pctx.HostJavaToolVariable("TurbineJar", "turbine.jar")
pctx.HostBinToolVariable("SoongJavacWrapper", "soong_javac_wrapper")

View file

@ -45,6 +45,7 @@ func makeVarsProvider(ctx android.MakeVarsContext) {
ctx.Strict("COMMON_JDK_FLAGS", "${CommonJdkFlags}")
ctx.Strict("DX", "${DxCmd}")
ctx.Strict("DX_COMMAND", "${DxCmd} -JXms16M -JXmx2048M")
ctx.Strict("TURBINE", "${TurbineJar}")
if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
ctx.Strict("TARGET_JAVAC", "${ErrorProneCmd}")

View file

@ -160,8 +160,11 @@ type Module struct {
protoProperties android.ProtoProperties
deviceProperties CompilerDeviceProperties
// output file suitable for inserting into the classpath of another compile
classpathFile android.Path
// header jar file suitable for inserting into the bootclasspath/classpath of another compile
headerJarFile android.Path
// full implementation jar file suitable for static dependency of another module compile
implementationJarFile android.Path
// output file containing classes.dex
dexJarFile android.Path
@ -182,7 +185,8 @@ type Module struct {
}
type Dependency interface {
ClasspathFiles() android.Paths
HeaderJars() android.Paths
ImplementationJars() android.Paths
AidlIncludeDirs() android.Paths
}
@ -379,6 +383,7 @@ type deps struct {
classpath android.Paths
bootClasspath android.Paths
staticJars android.Paths
staticHeaderJars android.Paths
staticJarResources android.Paths
aidlIncludeDirs android.Paths
srcJars android.Paths
@ -394,6 +399,7 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
if sdkDep.invalidVersion {
ctx.AddMissingDependencies([]string{sdkDep.module})
} else if sdkDep.useFiles {
// sdkDep.jar is actually equivalent to turbine header.jar.
deps.classpath = append(deps.classpath, sdkDep.jar)
deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
}
@ -424,12 +430,13 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
switch tag {
case bootClasspathTag:
deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
case libTag:
deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
deps.classpath = append(deps.classpath, dep.HeaderJars()...)
case staticLibTag:
deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
deps.classpath = append(deps.classpath, dep.HeaderJars()...)
deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
case frameworkResTag:
if ctx.ModuleName() == "framework" {
// framework.jar has a one-off dependency on the R.java and Manifest.java files
@ -437,7 +444,7 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
// TODO(ccross): aapt java files should go in a src jar
}
case kotlinStdlibTag:
deps.kotlinStdlib = dep.ClasspathFiles()
deps.kotlinStdlib = dep.HeaderJars()
default:
panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
}
@ -448,20 +455,22 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
return deps
}
func (j *Module) compile(ctx android.ModuleContext) {
j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
deps := j.collectDeps(ctx)
func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
var flags javaBuilderFlags
// javac flags.
javacFlags := j.properties.Javacflags
if ctx.AConfig().TargetOpenJDK9() {
javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
}
if len(javacFlags) > 0 {
// optimization.
ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
flags.javacFlags = "$javacFlags"
}
// javaVersion flag.
sdk := sdkStringToNumber(ctx, j.deviceProperties.Sdk_version)
if j.properties.Java_version != nil {
flags.javaVersion = *j.properties.Java_version
@ -473,35 +482,43 @@ func (j *Module) compile(ctx android.ModuleContext) {
flags.javaVersion = "1.9"
}
// classpath
flags.bootClasspath.AddPaths(deps.bootClasspath)
flags.classpath.AddPaths(deps.classpath)
// systemModules
if deps.systemModules != nil {
flags.systemModules = append(flags.systemModules, deps.systemModules)
}
if len(javacFlags) > 0 {
ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
flags.javacFlags = "$javacFlags"
}
// aidl flags.
aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
if len(aidlFlags) > 0 {
// optimization.
ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
flags.aidlFlags = "$aidlFlags"
}
srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
return flags
}
func (j *Module) compile(ctx android.ModuleContext) {
j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
deps := j.collectDeps(ctx)
flags := j.collectBuilderFlags(ctx, deps)
if ctx.AConfig().TargetOpenJDK9() {
j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
}
srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
if hasSrcExt(srcFiles.Strings(), ".proto") {
flags = protoFlags(ctx, &j.protoProperties, flags)
}
var srcJars classpath
srcFiles, srcJars = j.genSources(ctx, srcFiles, flags)
srcJars = append(srcJars, deps.srcJars...)
srcJars = append(srcJars, j.ExtraSrcJars...)
var jars android.Paths
@ -534,7 +551,27 @@ func (j *Module) compile(ctx android.ModuleContext) {
jars = append(jars, deps.kotlinStdlib...)
}
if javaSrcFiles := srcFiles.FilterByExt(".java"); len(javaSrcFiles) > 0 {
javaSrcFiles := srcFiles.FilterByExt(".java")
var uniqueSrcFiles android.Paths
set := make(map[string]bool)
for _, v := range javaSrcFiles {
if _, found := set[v.String()]; !found {
set[v.String()] = true
uniqueSrcFiles = append(uniqueSrcFiles, v)
}
}
if ctx.Device() && !ctx.AConfig().IsEnvFalse("TURBINE_ENABLED") {
// If sdk jar is java module, then directly return classesJar as header.jar
if j.Name() != "android_stubs_current" && j.Name() != "android_system_stubs_current" &&
j.Name() != "android_test_stubs_current" {
j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
if ctx.Failed() {
return
}
}
}
if len(uniqueSrcFiles) > 0 {
var extraJarDeps android.Paths
if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
// If error-prone is enabled, add an additional rule to compile the java files into
@ -591,7 +628,7 @@ func (j *Module) compile(ctx android.ModuleContext) {
manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
// Combine the classes built from sources, any manifests, and any static libraries into
// classes.jar. If there is only one input jar this step will be skipped.
// classes.jar. If there is only one input jar this step will be skipped.
var outputFile android.Path
if len(jars) == 1 && !manifest.Valid() {
@ -599,7 +636,7 @@ func (j *Module) compile(ctx android.ModuleContext) {
outputFile = jars[0]
} else {
combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
TransformJarsToJar(ctx, combinedJar, jars, manifest, false)
TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
outputFile = combinedJar
}
@ -613,86 +650,143 @@ func (j *Module) compile(ctx android.ModuleContext) {
return
}
}
j.classpathFile = outputFile
j.implementationJarFile = outputFile
if j.headerJarFile == nil {
j.headerJarFile = j.implementationJarFile
}
if ctx.Device() && j.installable() {
dxFlags := j.deviceProperties.Dxflags
if false /* emma enabled */ {
// If you instrument class files that have local variable debug information in
// them emma does not correctly maintain the local variable table.
// This will cause an error when you try to convert the class files for Android.
// The workaround here is to build different dex file here based on emma switch
// then later copy into classes.dex. When emma is on, dx is run with --no-locals
// option to remove local variable information
dxFlags = append(dxFlags, "--no-locals")
}
if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
dxFlags = append(dxFlags, "--no-optimize")
}
if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
dxFlags = append(dxFlags,
"--debug",
"--verbose",
"--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
"--dump-width=1000")
}
var minSdkVersion string
switch j.deviceProperties.Sdk_version {
case "", "current", "test_current", "system_current":
minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
default:
minSdkVersion = j.deviceProperties.Sdk_version
}
dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
flags.dxFlags = strings.Join(dxFlags, " ")
desugarFlags := []string{
"--min_sdk_version " + minSdkVersion,
"--desugar_try_with_resources_if_needed=false",
"--allow_empty_bootclasspath",
}
if inList("--core-library", dxFlags) {
desugarFlags = append(desugarFlags, "--core_library")
}
flags.desugarFlags = strings.Join(desugarFlags, " ")
desugarJar := android.PathForModuleOut(ctx, "desugar", jarName)
TransformDesugar(ctx, desugarJar, outputFile, flags)
outputFile = desugarJar
outputFile = j.compileDex(ctx, flags, outputFile, jarName)
if ctx.Failed() {
return
}
// Compile classes.jar into classes.dex and then a dex jar
dexJar := android.PathForModuleOut(ctx, "dex", jarName)
TransformClassesJarToDexJar(ctx, dexJar, desugarJar, flags)
outputFile = dexJar
if ctx.Failed() {
return
}
j.dexJarFile = outputFile
}
ctx.CheckbuildFile(outputFile)
j.outputFile = outputFile
}
func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles android.Paths, srcJars classpath,
deps deps, flags javaBuilderFlags, jarName string) android.Path {
var jars android.Paths
if len(srcFiles) > 0 {
// Compile java sources into turbine.jar.
turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
if ctx.Failed() {
return nil
}
jars = append(jars, turbineJar)
}
// Combine any static header libraries into classes-header.jar. If there is only
// one input jar this step will be skipped.
var headerJar android.Path
jars = append(jars, deps.staticHeaderJars...)
if len(jars) == 0 {
panic("The turbine.jar is empty without any sources and static libs.")
} else {
// we cannot skip the combine step for now if there is only one jar
// since we have to strip META-INF/TRANSITIVE dir from turbine.jar
combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
headerJar = combinedJar
}
if j.properties.Jarjar_rules != nil {
jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
// Transform classes.jar into classes-jarjar.jar
jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
headerJar = jarjarFile
if ctx.Failed() {
return nil
}
}
return headerJar
}
func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
classesJar android.Path, jarName string) android.Path {
dxFlags := j.deviceProperties.Dxflags
if false /* emma enabled */ {
// If you instrument class files that have local variable debug information in
// them emma does not correctly maintain the local variable table.
// This will cause an error when you try to convert the class files for Android.
// The workaround here is to build different dex file here based on emma switch
// then later copy into classes.dex. When emma is on, dx is run with --no-locals
// option to remove local variable information
dxFlags = append(dxFlags, "--no-locals")
}
if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
dxFlags = append(dxFlags, "--no-optimize")
}
if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
dxFlags = append(dxFlags,
"--debug",
"--verbose",
"--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
"--dump-width=1000")
}
var minSdkVersion string
switch j.deviceProperties.Sdk_version {
case "", "current", "test_current", "system_current":
minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
default:
minSdkVersion = j.deviceProperties.Sdk_version
}
dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
flags.dxFlags = strings.Join(dxFlags, " ")
desugarFlags := []string{
"--min_sdk_version " + minSdkVersion,
"--desugar_try_with_resources_if_needed=false",
"--allow_empty_bootclasspath",
}
if inList("--core-library", dxFlags) {
desugarFlags = append(desugarFlags, "--core_library")
}
flags.desugarFlags = strings.Join(desugarFlags, " ")
desugarJar := android.PathForModuleOut(ctx, "desugar", jarName)
TransformDesugar(ctx, desugarJar, classesJar, flags)
if ctx.Failed() {
return nil
}
// Compile classes.jar into classes.dex and then javalib.jar
javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
TransformClassesJarToDexJar(ctx, javalibJar, desugarJar, flags)
if ctx.Failed() {
return nil
}
j.dexJarFile = javalibJar
return javalibJar
}
func (j *Module) installable() bool {
return j.properties.Installable == nil || *j.properties.Installable
}
var _ Dependency = (*Library)(nil)
func (j *Module) ClasspathFiles() android.Paths {
return android.Paths{j.classpathFile}
func (j *Module) HeaderJars() android.Paths {
return android.Paths{j.headerJarFile}
}
func (j *Module) ImplementationJars() android.Paths {
return android.Paths{j.implementationJarFile}
}
func (j *Module) AidlIncludeDirs() android.Paths {
@ -850,13 +944,17 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
outputFile := android.PathForModuleOut(ctx, "classes.jar")
TransformJarsToJar(ctx, outputFile, j.classpathFiles, android.OptionalPath{}, false)
TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
j.combinedClasspathFile = outputFile
}
var _ Dependency = (*Import)(nil)
func (j *Import) ClasspathFiles() android.Paths {
func (j *Import) HeaderJars() android.Paths {
return j.classpathFiles
}
func (j *Import) ImplementationJars() android.Paths {
return j.classpathFiles
}

View file

@ -141,8 +141,11 @@ func moduleToPath(name string) string {
return name
case strings.HasSuffix(name, ".jar"):
return name
default:
case name == "android_stubs_current" || name == "android_system_stubs_current" ||
name == "android_test_stubs_current":
return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
default:
return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
}
}
@ -167,21 +170,22 @@ func TestSimple(t *testing.T) {
`)
javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
}
bar := ctx.ModuleForTests("bar", "android_common").Rule("javac").Output.String()
baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
if !strings.Contains(javac.Args["classpath"], bar) {
t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
if !strings.Contains(javac.Args["classpath"], barTurbine) {
t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
}
if !strings.Contains(javac.Args["classpath"], baz) {
t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], baz)
if !strings.Contains(javac.Args["classpath"], bazTurbine) {
t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
}
if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
@ -421,7 +425,7 @@ func TestPrebuilts(t *testing.T) {
`)
javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
bar := "a.jar"
if !strings.Contains(javac.Args["classpath"], bar) {
@ -459,15 +463,15 @@ func TestDefaults(t *testing.T) {
`)
javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
}
bar := ctx.ModuleForTests("bar", "android_common").Rule("javac").Output.String()
if !strings.Contains(javac.Args["classpath"], bar) {
t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
if !strings.Contains(javac.Args["classpath"], barTurbine) {
t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
}
baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()

View file

@ -115,7 +115,7 @@ func (system *SystemModules) GenerateAndroidBuildActions(ctx android.ModuleConte
ctx.VisitDirectDeps(func(module blueprint.Module) {
if ctx.OtherModuleDependencyTag(module) == libTag {
dep, _ := module.(Dependency)
jars = append(jars, dep.ClasspathFiles()...)
jars = append(jars, dep.HeaderJars()...)
}
})