2019-01-19 07:05:09 +08:00
|
|
|
// Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
2019-01-18 07:42:52 +08:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
2019-05-09 05:30:12 +08:00
|
|
|
"path/filepath"
|
2019-01-19 07:05:09 +08:00
|
|
|
"strings"
|
|
|
|
|
2019-01-18 07:42:52 +08:00
|
|
|
"android/soong/android"
|
|
|
|
|
2019-01-19 07:05:09 +08:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
)
|
|
|
|
|
2019-11-05 07:16:11 +08:00
|
|
|
var kotlinc = pctx.AndroidRemoteStaticRule("kotlinc", android.SUPPORTS_GOMA,
|
2019-01-19 07:05:09 +08:00
|
|
|
blueprint.RuleParams{
|
2019-04-10 06:29:41 +08:00
|
|
|
Command: `rm -rf "$classesDir" "$srcJarDir" "$kotlinBuildFile" "$emptyDir" && ` +
|
|
|
|
`mkdir -p "$classesDir" "$srcJarDir" "$emptyDir" && ` +
|
2019-01-19 07:05:09 +08:00
|
|
|
`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
|
2019-05-09 05:30:12 +08:00
|
|
|
`${config.GenKotlinBuildFileCmd} $classpath "$name" $classesDir $out.rsp $srcJarDir/list > $kotlinBuildFile &&` +
|
2019-01-18 07:42:52 +08:00
|
|
|
`${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` +
|
2019-04-10 06:29:41 +08:00
|
|
|
`-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile -kotlin-home $emptyDir && ` +
|
2019-01-25 08:36:57 +08:00
|
|
|
`${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` +
|
|
|
|
`rm -rf "$srcJarDir"`,
|
2019-01-19 07:05:09 +08:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.KotlincCmd}",
|
|
|
|
"${config.KotlinCompilerJar}",
|
2019-04-08 00:44:35 +08:00
|
|
|
"${config.KotlinPreloaderJar}",
|
|
|
|
"${config.KotlinReflectJar}",
|
|
|
|
"${config.KotlinScriptRuntimeJar}",
|
|
|
|
"${config.KotlinStdlibJar}",
|
|
|
|
"${config.KotlinTrove4jJar}",
|
2019-06-13 12:59:17 +08:00
|
|
|
"${config.KotlinAnnotationJar}",
|
2019-01-19 07:05:09 +08:00
|
|
|
"${config.GenKotlinBuildFileCmd}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.ZipSyncCmd}",
|
|
|
|
},
|
|
|
|
Rspfile: "$out.rsp",
|
|
|
|
RspfileContent: `$in`,
|
|
|
|
},
|
2019-05-09 05:30:12 +08:00
|
|
|
"kotlincFlags", "classpath", "srcJars", "srcJarDir", "classesDir", "kotlinJvmTarget", "kotlinBuildFile",
|
|
|
|
"emptyDir", "name")
|
2019-01-19 07:05:09 +08:00
|
|
|
|
2019-01-18 07:42:52 +08:00
|
|
|
// kotlinCompile takes .java and .kt sources and srcJars, and compiles the .kt sources into a classes jar in outputFile.
|
2019-01-19 07:05:09 +08:00
|
|
|
func kotlinCompile(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...)
|
|
|
|
|
2019-05-09 05:30:12 +08:00
|
|
|
kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
|
|
|
|
kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
|
|
|
|
|
2019-01-19 07:05:09 +08:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: kotlinc,
|
|
|
|
Description: "kotlinc",
|
|
|
|
Output: outputFile,
|
2019-01-18 07:42:52 +08:00
|
|
|
Inputs: srcFiles,
|
2019-01-19 07:05:09 +08:00
|
|
|
Implicits: deps,
|
|
|
|
Args: map[string]string{
|
|
|
|
"classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
|
|
|
|
"kotlincFlags": flags.kotlincFlags,
|
|
|
|
"srcJars": strings.Join(srcJars.Strings(), " "),
|
|
|
|
"classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
|
|
|
|
"srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
|
|
|
|
"kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(),
|
2019-04-10 06:29:41 +08:00
|
|
|
"emptyDir": android.PathForModuleOut(ctx, "kotlinc", "empty").String(),
|
2019-01-19 07:05:09 +08:00
|
|
|
// http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
|
|
|
|
"kotlinJvmTarget": "1.8",
|
2019-05-09 05:30:12 +08:00
|
|
|
"name": kotlinName,
|
2019-01-19 07:05:09 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2019-01-18 07:42:52 +08:00
|
|
|
|
2019-11-05 07:16:11 +08:00
|
|
|
var kapt = pctx.AndroidRemoteStaticRule("kapt", android.SUPPORTS_GOMA,
|
2019-01-18 07:42:52 +08:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && mkdir -p "$srcJarDir" "$kaptDir" && ` +
|
|
|
|
`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
|
2019-05-09 05:30:12 +08:00
|
|
|
`${config.GenKotlinBuildFileCmd} $classpath "$name" "" $out.rsp $srcJarDir/list > $kotlinBuildFile &&` +
|
2019-01-18 07:42:52 +08:00
|
|
|
`${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 ` +
|
2019-01-22 13:37:16 +08:00
|
|
|
`$kaptProcessor ` +
|
2019-01-18 07:42:52 +08:00
|
|
|
`-Xbuild-file=$kotlinBuildFile && ` +
|
2019-01-25 08:36:57 +08:00
|
|
|
`${config.SoongZipCmd} -jar -o $out -C $kaptDir/sources -D $kaptDir/sources && ` +
|
|
|
|
`rm -rf "$srcJarDir"`,
|
2019-01-18 07:42:52 +08:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.KotlincCmd}",
|
|
|
|
"${config.KotlinCompilerJar}",
|
|
|
|
"${config.KotlinKaptJar}",
|
|
|
|
"${config.GenKotlinBuildFileCmd}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.ZipSyncCmd}",
|
|
|
|
},
|
|
|
|
Rspfile: "$out.rsp",
|
|
|
|
RspfileContent: `$in`,
|
|
|
|
},
|
2019-01-22 13:37:16 +08:00
|
|
|
"kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor",
|
2019-05-09 05:30:12 +08:00
|
|
|
"classpath", "srcJars", "srcJarDir", "kaptDir", "kotlinJvmTarget", "kotlinBuildFile", "name")
|
2019-01-18 07:42:52 +08:00
|
|
|
|
|
|
|
// 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=")
|
|
|
|
|
2019-01-22 13:37:16 +08:00
|
|
|
kaptProcessor := ""
|
|
|
|
if flags.processor != "" {
|
2019-01-24 07:39:50 +08:00
|
|
|
kaptProcessor = "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + flags.processor
|
2019-01-22 13:37:16 +08:00
|
|
|
}
|
|
|
|
|
2019-01-18 07:42:52 +08:00
|
|
|
encodedJavacFlags := kaptEncodeFlags([][2]string{
|
2019-10-29 02:37:20 +08:00
|
|
|
{"-source", flags.javaVersion.String()},
|
|
|
|
{"-target", flags.javaVersion.String()},
|
2019-01-18 07:42:52 +08:00
|
|
|
})
|
|
|
|
|
2019-05-09 05:30:12 +08:00
|
|
|
kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
|
|
|
|
kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
|
|
|
|
|
2019-01-18 07:42:52 +08:00
|
|
|
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, " "),
|
2019-01-22 13:37:16 +08:00
|
|
|
"kaptProcessor": kaptProcessor,
|
2019-01-18 07:42:52 +08:00
|
|
|
"kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(),
|
|
|
|
"encodedJavacFlags": encodedJavacFlags,
|
2019-05-09 05:30:12 +08:00
|
|
|
"name": kotlinName,
|
2019-01-18 07:42:52 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()...))
|
|
|
|
}
|