aboutsummaryrefslogtreecommitdiffstats
path: root/java/kotlin.go
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2019-01-17 15:42:52 -0800
committerColin Cross <ccross@android.com>2019-01-22 21:47:19 +0000
commitafbb1734f6fe8073885961578e9312e16826a2e8 (patch)
tree1cb607eccd00db64b72aa306c7b1649d25457c31 /java/kotlin.go
parent21fc9bbe198caf9736bb9dc0f5701e8bfd56de40 (diff)
downloadbuild_soong-afbb1734f6fe8073885961578e9312e16826a2e8.tar.gz
build_soong-afbb1734f6fe8073885961578e9312e16826a2e8.tar.bz2
build_soong-afbb1734f6fe8073885961578e9312e16826a2e8.zip
Pass annotation processors to kotlinc
Enable the kotlin-annotation-processing plugin and pass annotation processors to it. Bug: 122251693 Test: m checkbuild Test: TestKapt in kotlin_test.go Change-Id: I841df454beaaa7edd263eea714ca0d958a03c9de
Diffstat (limited to 'java/kotlin.go')
-rw-r--r--java/kotlin.go107
1 files changed, 102 insertions, 5 deletions
diff --git a/java/kotlin.go b/java/kotlin.go
index 5bc7441c..c72f2de0 100644
--- a/java/kotlin.go
+++ b/java/kotlin.go
@@ -15,9 +15,13 @@
package java
import (
- "android/soong/android"
+ "bytes"
+ "encoding/base64"
+ "encoding/binary"
"strings"
+ "android/soong/android"
+
"github.com/google/blueprint"
)
@@ -26,7 +30,7 @@ var kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
Command: `rm -rf "$classesDir" "$srcJarDir" "$kotlinBuildFile" && mkdir -p "$classesDir" "$srcJarDir" && ` +
`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
`${config.GenKotlinBuildFileCmd} $classpath $classesDir $out.rsp $srcJarDir/list > $kotlinBuildFile &&` +
- `${config.KotlincCmd} $kotlincFlags ` +
+ `${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` +
`-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile && ` +
`${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir`,
CommandDeps: []string{
@@ -41,12 +45,11 @@ var kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
},
"kotlincFlags", "classpath", "srcJars", "srcJarDir", "classesDir", "kotlinJvmTarget", "kotlinBuildFile")
+// kotlinCompile takes .java and .kt sources and srcJars, and compiles the .kt sources into a classes jar in outputFile.
func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath,
srcFiles, srcJars android.Paths,
flags javaBuilderFlags) {
- inputs := append(android.Paths(nil), srcFiles...)
-
var deps android.Paths
deps = append(deps, flags.kotlincClasspath...)
deps = append(deps, srcJars...)
@@ -55,7 +58,7 @@ func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath,
Rule: kotlinc,
Description: "kotlinc",
Output: outputFile,
- Inputs: inputs,
+ Inputs: srcFiles,
Implicits: deps,
Args: map[string]string{
"classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
@@ -69,3 +72,97 @@ func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath,
},
})
}
+
+var kapt = pctx.AndroidGomaStaticRule("kapt",
+ blueprint.RuleParams{
+ Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && mkdir -p "$srcJarDir" "$kaptDir" && ` +
+ `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
+ `${config.GenKotlinBuildFileCmd} $classpath "" $out.rsp $srcJarDir/list > $kotlinBuildFile &&` +
+ `${config.KotlincCmd} ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} $kotlincFlags ` +
+ `-Xplugin=${config.KotlinKaptJar} ` +
+ `-P plugin:org.jetbrains.kotlin.kapt3:sources=$kaptDir/sources ` +
+ `-P plugin:org.jetbrains.kotlin.kapt3:classes=$kaptDir/classes ` +
+ `-P plugin:org.jetbrains.kotlin.kapt3:stubs=$kaptDir/stubs ` +
+ `-P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true ` +
+ `-P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt ` +
+ `-P plugin:org.jetbrains.kotlin.kapt3:javacArguments=$encodedJavacFlags ` +
+ `$kaptProcessorPath ` +
+ `-Xbuild-file=$kotlinBuildFile && ` +
+ `${config.SoongZipCmd} -jar -o $out -C $kaptDir/sources -D $kaptDir/sources`,
+ CommandDeps: []string{
+ "${config.KotlincCmd}",
+ "${config.KotlinCompilerJar}",
+ "${config.KotlinKaptJar}",
+ "${config.GenKotlinBuildFileCmd}",
+ "${config.SoongZipCmd}",
+ "${config.ZipSyncCmd}",
+ },
+ Rspfile: "$out.rsp",
+ RspfileContent: `$in`,
+ },
+ "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "classpath", "srcJars", "srcJarDir", "kaptDir", "kotlinJvmTarget", "kotlinBuildFile")
+
+// kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs
+// annotation processors over all of them, producing a srcjar of generated code in outputFile. The srcjar should be
+// added as an additional input to kotlinc and javac rules, and the javac rule should have annotation processing
+// disabled.
+func kotlinKapt(ctx android.ModuleContext, outputFile android.WritablePath,
+ srcFiles, srcJars android.Paths,
+ flags javaBuilderFlags) {
+
+ var deps android.Paths
+ deps = append(deps, flags.kotlincClasspath...)
+ deps = append(deps, srcJars...)
+ deps = append(deps, flags.processorPath...)
+
+ kaptProcessorPath := flags.processorPath.FormTurbineClasspath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=")
+
+ encodedJavacFlags := kaptEncodeFlags([][2]string{
+ {"-source", flags.javaVersion},
+ {"-target", flags.javaVersion},
+ })
+
+ ctx.Build(pctx, android.BuildParams{
+ Rule: kapt,
+ Description: "kapt",
+ Output: outputFile,
+ Inputs: srcFiles,
+ Implicits: deps,
+ Args: map[string]string{
+ "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
+ "kotlincFlags": flags.kotlincFlags,
+ "srcJars": strings.Join(srcJars.Strings(), " "),
+ "srcJarDir": android.PathForModuleOut(ctx, "kapt", "srcJars").String(),
+ "kotlinBuildFile": android.PathForModuleOut(ctx, "kapt", "build.xml").String(),
+ "kaptProcessorPath": strings.Join(kaptProcessorPath, " "),
+ "kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(),
+ "encodedJavacFlags": encodedJavacFlags,
+ },
+ })
+}
+
+// kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects.
+func kaptEncodeFlags(options [][2]string) string {
+ buf := &bytes.Buffer{}
+
+ binary.Write(buf, binary.BigEndian, uint32(len(options)))
+ for _, option := range options {
+ binary.Write(buf, binary.BigEndian, uint16(len(option[0])))
+ buf.WriteString(option[0])
+ binary.Write(buf, binary.BigEndian, uint16(len(option[1])))
+ buf.WriteString(option[1])
+ }
+
+ header := &bytes.Buffer{}
+ header.Write([]byte{0xac, 0xed, 0x00, 0x05}) // java serialization header
+
+ if buf.Len() < 256 {
+ header.WriteByte(0x77) // blockdata
+ header.WriteByte(byte(buf.Len()))
+ } else {
+ header.WriteByte(0x7a) // blockdatalong
+ binary.Write(header, binary.BigEndian, uint32(buf.Len()))
+ }
+
+ return base64.StdEncoding.EncodeToString(append(header.Bytes(), buf.Bytes()...))
+}