Initial kotlin support

Allow java libraries to specify .kt sources, precompile them with
kotlin, and then pass them in the classpath to javac.

Bug: 65219535
Test: java_test.go
Change-Id: Ife22b6ef82ced9ec26a9e5392b2dadacbb16546f
This commit is contained in:
Colin Cross
2017-08-15 13:34:18 -07:00
parent f6df17afd1
commit 93e8595044
6 changed files with 174 additions and 3 deletions

View File

@@ -198,6 +198,7 @@ var (
bootClasspathTag = dependencyTag{name: "bootclasspath"}
systemModulesTag = dependencyTag{name: "system modules"}
frameworkResTag = dependencyTag{name: "framework-res"}
kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
)
type sdkDep struct {
@@ -326,6 +327,12 @@ func (j *Module) deps(ctx android.BottomUpMutatorContext) {
if j.hasSrcExt(".proto") {
protoDeps(ctx, &j.protoProperties)
}
if j.hasSrcExt(".kt") {
// TODO(ccross): move this to a mutator pass that can tell if generated sources contain
// Kotlin files
ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
}
}
func hasSrcExt(srcs []string, ext string) bool {
@@ -373,6 +380,7 @@ type deps struct {
srcJars android.Paths
systemModules android.Path
aidlPreprocess android.OptionalPath
kotlinStdlib android.Paths
}
func (j *Module) collectDeps(ctx android.ModuleContext) deps {
@@ -424,6 +432,8 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
// generated by framework-res.apk
// TODO(ccross): aapt java files should go in a src jar
}
case kotlinStdlibTag:
deps.kotlinStdlib = dep.ClasspathFiles()
default:
panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
}
@@ -492,7 +502,33 @@ func (j *Module) compile(ctx android.ModuleContext) {
var jars android.Paths
if len(srcFiles) > 0 {
if srcFiles.HasExt(".kt") {
// If there are kotlin files, compile them first but pass all the kotlin and java files
// kotlinc will use the java files to resolve types referenced by the kotlin files, but
// won't emit any classes for them.
flags.kotlincFlags = "-no-stdlib"
if ctx.Device() {
flags.kotlincFlags += " -no-jdk"
}
flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
kotlinJar := android.PathForModuleOut(ctx, "classes-kt.jar")
TransformKotlinToClasses(ctx, kotlinJar, srcFiles, srcJars, flags)
if ctx.Failed() {
return
}
// Make javac rule depend on the kotlinc rule
flags.classpath = append(flags.classpath, kotlinJar)
// Jar kotlin classes into the final jar after javac
jars = append(jars, kotlinJar)
jars = append(jars, deps.kotlinStdlib...)
}
if javaSrcFiles := srcFiles.FilterByExt(".java"); len(javaSrcFiles) > 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
@@ -501,13 +537,13 @@ func (j *Module) compile(ctx android.ModuleContext) {
// TODO(ccross): Once we always compile with javac9 we may be able to conditionally
// enable error-prone without affecting the output class files.
errorprone := android.PathForModuleOut(ctx, "classes-errorprone.list")
RunErrorProne(ctx, errorprone, srcFiles, srcJars, flags)
RunErrorProne(ctx, errorprone, javaSrcFiles, srcJars, flags)
extraJarDeps = append(extraJarDeps, errorprone)
}
// Compile java sources into .class files
classes := android.PathForModuleOut(ctx, "classes-compiled.jar")
TransformJavaToClasses(ctx, classes, srcFiles, srcJars, flags, extraJarDeps)
TransformJavaToClasses(ctx, classes, javaSrcFiles, srcJars, flags, extraJarDeps)
if ctx.Failed() {
return
}