Use kapt stubs for kotlin header jar for javac

When compling kotlin code with annotation processors we are generating
java stubs for the kotlin sources using kapt, and parsing them in
turbine when running the annotation processors.  Passing --output
to turbine will also compile the stubs into a header jar that can
be used as a dependency to javac, allowing the javac and kotlinc
rules to run in parallel.  The turbine-apt header jar can't be used
as the header jar for downstream modules as it doesn't contain the
kotlin metadata needed by kotlinc rules, so the kotlinc gen-jvm-abi
plugin output is still used for the module's final header jar.

Test: TestKapt
Bug: 222095735
Change-Id: I82d0900f3dc30f3e3ebd7cab0693dfe741d9b955
This commit is contained in:
Colin Cross
2022-03-28 18:27:17 -07:00
parent b5db401599
commit c6ef485370
4 changed files with 51 additions and 24 deletions

View File

@@ -1108,13 +1108,23 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
if len(flags.processorPath) > 0 {
useTurbineApt := len(flags.processorPath) > 0
if useTurbineApt {
// Use kapt for annotation processing
kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
kaptResJar := android.PathForModuleOut(ctx, "kapt", "kapt-res.jar")
kotlinKapt(ctx, kaptSrcJar, kaptResJar, kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
srcJars = append(srcJars, kaptSrcJar)
kotlinJars = append(kotlinJars, kaptResJar)
kotlinTurbineAptHeaderJar := android.PathForModuleOut(ctx, "turbine-apt", "stubs-header.jar")
kotlinTurbineAptSrcJar := android.PathForModuleOut(ctx, "turbine-apt", "anno-sources.jar")
kotlinTurbineAptResJar := android.PathForModuleOut(ctx, "turbine-apt", "anno-res.jar")
kotlinTurbineApt(ctx, kotlinTurbineAptHeaderJar, kotlinTurbineAptSrcJar, kotlinTurbineAptResJar,
kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
srcJars = append(srcJars, kotlinTurbineAptSrcJar)
kotlinJars = append(kotlinJars, kotlinTurbineAptResJar)
// When annotation processors are enabled we've already created java stubs for
// kotlin files using kapt and compiled them in turbine-apt while running the
// annotation processors, reuse the result as the kotlin header jar for the javac
// action. It can't be used as the header jar for downstream modules to compile
// against because it doesn't contain the kotlin-specific metadata that kotlinc
// needs.
flags.classpath = append(classpath{kotlinTurbineAptHeaderJar}, flags.classpath...)
// Disable annotation processing in javac, it's already been handled by kapt
flags.processorPath = nil
flags.processors = nil
@@ -1127,11 +1137,13 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
return
}
// Make javac rule depend on the kotlinc rule
flags.classpath = append(classpath{kotlinHeaderJar}, flags.classpath...)
kotlinJars = append(kotlinJars, kotlinJar)
kotlinHeaderJars = append(kotlinHeaderJars, kotlinHeaderJar)
if !useTurbineApt {
// When annotation processors are not enabled use the kotlinc gen-jvm-abi plugin
// output as the header jar for javac in this module.
flags.classpath = append(classpath{kotlinHeaderJar}, flags.classpath...)
}
// Jar kotlin classes into the final jar after javac
if BoolDefault(j.properties.Static_kotlin_stdlib, true) {