2019-01-19 00:05:09 +01: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 00:42:52 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
2019-05-08 23:30:12 +02:00
|
|
|
"path/filepath"
|
2019-01-19 00:05:09 +01:00
|
|
|
"strings"
|
|
|
|
|
2019-01-18 00:42:52 +01:00
|
|
|
"android/soong/android"
|
|
|
|
|
2019-01-19 00:05:09 +01:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
)
|
|
|
|
|
2020-01-27 20:19:44 +01:00
|
|
|
var kotlinc = pctx.AndroidRemoteStaticRule("kotlinc", android.RemoteRuleSupports{Goma: true},
|
2019-01-19 00:05:09 +01:00
|
|
|
blueprint.RuleParams{
|
2022-03-29 02:08:01 +02:00
|
|
|
Command: `rm -rf "$classesDir" "$headerClassesDir" "$srcJarDir" "$kotlinBuildFile" "$emptyDir" && ` +
|
|
|
|
`mkdir -p "$classesDir" "$headerClassesDir" "$srcJarDir" "$emptyDir" && ` +
|
2023-06-13 19:40:52 +02:00
|
|
|
`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" -f "*.kt" $srcJars && ` +
|
2020-06-26 02:12:28 +02:00
|
|
|
`${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
|
|
|
|
` --out_dir "$classesDir" --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
|
2020-06-26 02:13:36 +02:00
|
|
|
` $commonSrcFilesArg --out "$kotlinBuildFile" && ` +
|
2022-03-17 20:01:27 +01:00
|
|
|
`${config.KotlincCmd} ${config.KotlincGlobalFlags} ` +
|
2022-03-29 02:08:01 +02:00
|
|
|
` ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} ` +
|
|
|
|
` $kotlincFlags -jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile ` +
|
|
|
|
` -kotlin-home $emptyDir ` +
|
|
|
|
` -Xplugin=${config.KotlinAbiGenPluginJar} ` +
|
|
|
|
` -P plugin:org.jetbrains.kotlin.jvm.abi:outputDir=$headerClassesDir && ` +
|
|
|
|
`${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir -write_if_changed && ` +
|
|
|
|
`${config.SoongZipCmd} -jar -o $headerJar -C $headerClassesDir -D $headerClassesDir -write_if_changed && ` +
|
2019-01-25 01:36:57 +01:00
|
|
|
`rm -rf "$srcJarDir"`,
|
2019-01-19 00:05:09 +01:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.KotlincCmd}",
|
|
|
|
"${config.KotlinCompilerJar}",
|
2019-04-07 18:44:35 +02:00
|
|
|
"${config.KotlinPreloaderJar}",
|
|
|
|
"${config.KotlinReflectJar}",
|
|
|
|
"${config.KotlinScriptRuntimeJar}",
|
|
|
|
"${config.KotlinStdlibJar}",
|
|
|
|
"${config.KotlinTrove4jJar}",
|
2019-06-13 06:59:17 +02:00
|
|
|
"${config.KotlinAnnotationJar}",
|
2022-03-29 02:08:01 +02:00
|
|
|
"${config.KotlinAbiGenPluginJar}",
|
2019-01-19 00:05:09 +01:00
|
|
|
"${config.GenKotlinBuildFileCmd}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.ZipSyncCmd}",
|
|
|
|
},
|
|
|
|
Rspfile: "$out.rsp",
|
|
|
|
RspfileContent: `$in`,
|
2022-03-29 02:08:01 +02:00
|
|
|
Restat: true,
|
2019-01-19 00:05:09 +01:00
|
|
|
},
|
2020-06-26 02:13:36 +02:00
|
|
|
"kotlincFlags", "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "classesDir",
|
2022-03-29 02:08:01 +02:00
|
|
|
"headerClassesDir", "headerJar", "kotlinJvmTarget", "kotlinBuildFile", "emptyDir", "name")
|
2020-06-26 02:13:36 +02:00
|
|
|
|
|
|
|
func kotlinCommonSrcsList(ctx android.ModuleContext, commonSrcFiles android.Paths) android.OptionalPath {
|
|
|
|
if len(commonSrcFiles) > 0 {
|
|
|
|
// The list of common_srcs may be too long to put on the command line, but
|
|
|
|
// we can't use the rsp file because it is already being used for srcs.
|
|
|
|
// Insert a second rule to write out the list of resources to a file.
|
|
|
|
commonSrcsList := android.PathForModuleOut(ctx, "kotlinc_common_srcs.list")
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := android.NewRuleBuilder(pctx, ctx)
|
2021-03-13 02:48:14 +01:00
|
|
|
rule.Command().Text("cp").
|
|
|
|
FlagWithRspFileInputList("", commonSrcsList.ReplaceExtension(ctx, "rsp"), commonSrcFiles).
|
|
|
|
Output(commonSrcsList)
|
2020-11-17 02:32:30 +01:00
|
|
|
rule.Build("kotlin_common_srcs_list", "kotlin common_srcs list")
|
2020-06-26 02:13:36 +02:00
|
|
|
return android.OptionalPathForPath(commonSrcsList)
|
|
|
|
}
|
|
|
|
return android.OptionalPath{}
|
|
|
|
}
|
2019-01-19 00:05:09 +01:00
|
|
|
|
2019-01-18 00:42:52 +01:00
|
|
|
// kotlinCompile takes .java and .kt sources and srcJars, and compiles the .kt sources into a classes jar in outputFile.
|
2022-03-29 02:08:01 +02:00
|
|
|
func kotlinCompile(ctx android.ModuleContext, outputFile, headerOutputFile android.WritablePath,
|
2020-06-26 02:13:36 +02:00
|
|
|
srcFiles, commonSrcFiles, srcJars android.Paths,
|
2019-01-19 00:05:09 +01:00
|
|
|
flags javaBuilderFlags) {
|
|
|
|
|
|
|
|
var deps android.Paths
|
|
|
|
deps = append(deps, flags.kotlincClasspath...)
|
2021-09-17 23:11:52 +02:00
|
|
|
deps = append(deps, flags.kotlincDeps...)
|
2019-01-19 00:05:09 +01:00
|
|
|
deps = append(deps, srcJars...)
|
2020-06-26 02:13:36 +02:00
|
|
|
deps = append(deps, commonSrcFiles...)
|
2019-01-19 00:05:09 +01:00
|
|
|
|
2019-05-08 23:30:12 +02:00
|
|
|
kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
|
|
|
|
kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
|
|
|
|
|
2020-06-26 02:13:36 +02:00
|
|
|
commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
|
|
|
|
commonSrcFilesArg := ""
|
|
|
|
if commonSrcsList.Valid() {
|
|
|
|
deps = append(deps, commonSrcsList.Path())
|
|
|
|
commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
|
|
|
|
}
|
|
|
|
|
2019-01-19 00:05:09 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2022-03-29 02:08:01 +02:00
|
|
|
Rule: kotlinc,
|
|
|
|
Description: "kotlinc",
|
|
|
|
Output: outputFile,
|
|
|
|
ImplicitOutput: headerOutputFile,
|
|
|
|
Inputs: srcFiles,
|
|
|
|
Implicits: deps,
|
2019-01-19 00:05:09 +01:00
|
|
|
Args: map[string]string{
|
2020-06-26 02:13:36 +02:00
|
|
|
"classpath": flags.kotlincClasspath.FormJavaClassPath(""),
|
|
|
|
"kotlincFlags": flags.kotlincFlags,
|
|
|
|
"commonSrcFilesArg": commonSrcFilesArg,
|
|
|
|
"srcJars": strings.Join(srcJars.Strings(), " "),
|
|
|
|
"classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
|
2022-03-29 02:08:01 +02:00
|
|
|
"headerClassesDir": android.PathForModuleOut(ctx, "kotlinc", "header_classes").String(),
|
|
|
|
"headerJar": headerOutputFile.String(),
|
2020-06-26 02:13:36 +02:00
|
|
|
"srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
|
|
|
|
"kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(),
|
|
|
|
"emptyDir": android.PathForModuleOut(ctx, "kotlinc", "empty").String(),
|
Use the current java version for -jvm-target
This is a partial revert of aosp/541879.
In that cl, the kotlin jvm version was always
set to 1.8 because that's what the kotlin
complier supported at the time, but now
it supports more versions:
$ ./external/kotlinc/bin/kotlinc -jvm-target foo
error: unknown JVM target version: foo
Supported versions: 1.6, 1.8, 9, 10, 11, 12, 13, 14, 15, 16, 17
Bug: 69160377
Bug: 236431222
Test: Treehugger
Change-Id: I273e0b4d1f484013889e17c60bc1b299a3f975a1
2022-06-28 23:41:27 +02:00
|
|
|
"kotlinJvmTarget": flags.javaVersion.StringForKotlinc(),
|
|
|
|
"name": kotlinName,
|
2019-01-19 00:05:09 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2019-01-18 00:42:52 +01:00
|
|
|
|
2022-03-17 02:06:48 +01:00
|
|
|
var kaptStubs = pctx.AndroidRemoteStaticRule("kaptStubs", android.RemoteRuleSupports{Goma: true},
|
2019-01-18 00:42:52 +01:00
|
|
|
blueprint.RuleParams{
|
2020-06-19 00:46:32 +02:00
|
|
|
Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && ` +
|
|
|
|
`mkdir -p "$srcJarDir" "$kaptDir/sources" "$kaptDir/classes" && ` +
|
2019-01-18 00:42:52 +01:00
|
|
|
`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
|
2020-06-26 02:12:28 +02:00
|
|
|
`${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
|
|
|
|
` --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
|
2020-06-26 02:13:36 +02:00
|
|
|
` $commonSrcFilesArg --out "$kotlinBuildFile" && ` +
|
2022-03-17 20:01:27 +01:00
|
|
|
`${config.KotlincCmd} ${config.KotlincGlobalFlags} ` +
|
|
|
|
`${config.KaptSuppressJDK9Warnings} ${config.KotlincSuppressJDK9Warnings} ` +
|
2021-03-26 18:52:02 +01:00
|
|
|
`${config.JavacHeapFlags} $kotlincFlags -Xplugin=${config.KotlinKaptJar} ` +
|
2019-01-18 00:42:52 +01:00
|
|
|
`-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 ` +
|
2022-03-17 02:06:48 +01:00
|
|
|
`-P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubs ` +
|
2019-01-18 00:42:52 +01:00
|
|
|
`-P plugin:org.jetbrains.kotlin.kapt3:javacArguments=$encodedJavacFlags ` +
|
|
|
|
`$kaptProcessorPath ` +
|
2019-01-22 06:37:16 +01:00
|
|
|
`$kaptProcessor ` +
|
2019-01-18 00:42:52 +01:00
|
|
|
`-Xbuild-file=$kotlinBuildFile && ` +
|
2022-03-17 02:06:48 +01:00
|
|
|
`${config.SoongZipCmd} -jar -o $out -C $kaptDir/stubs -D $kaptDir/stubs && ` +
|
2019-01-25 01:36:57 +01:00
|
|
|
`rm -rf "$srcJarDir"`,
|
2019-01-18 00:42:52 +01:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.KotlincCmd}",
|
|
|
|
"${config.KotlinCompilerJar}",
|
|
|
|
"${config.KotlinKaptJar}",
|
|
|
|
"${config.GenKotlinBuildFileCmd}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.ZipSyncCmd}",
|
|
|
|
},
|
|
|
|
Rspfile: "$out.rsp",
|
|
|
|
RspfileContent: `$in`,
|
|
|
|
},
|
2019-01-22 06:37:16 +01:00
|
|
|
"kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor",
|
2020-06-26 02:13:36 +02:00
|
|
|
"classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "kaptDir", "kotlinJvmTarget",
|
|
|
|
"kotlinBuildFile", "name", "classesJarOut")
|
2019-01-18 00:42:52 +01:00
|
|
|
|
2022-04-06 08:14:38 +02:00
|
|
|
// kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs
|
2019-01-18 00:42:52 +01:00
|
|
|
// 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.
|
2022-04-06 08:14:38 +02:00
|
|
|
func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile android.WritablePath,
|
2020-06-26 02:13:36 +02:00
|
|
|
srcFiles, commonSrcFiles, srcJars android.Paths,
|
2019-01-18 00:42:52 +01:00
|
|
|
flags javaBuilderFlags) {
|
|
|
|
|
2020-06-26 02:13:36 +02:00
|
|
|
srcFiles = append(android.Paths(nil), srcFiles...)
|
|
|
|
|
2019-01-18 00:42:52 +01:00
|
|
|
var deps android.Paths
|
|
|
|
deps = append(deps, flags.kotlincClasspath...)
|
2022-05-02 22:59:33 +02:00
|
|
|
deps = append(deps, flags.kotlincDeps...)
|
2019-01-18 00:42:52 +01:00
|
|
|
deps = append(deps, srcJars...)
|
|
|
|
deps = append(deps, flags.processorPath...)
|
2020-06-26 02:13:36 +02:00
|
|
|
deps = append(deps, commonSrcFiles...)
|
|
|
|
|
|
|
|
commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
|
|
|
|
commonSrcFilesArg := ""
|
|
|
|
if commonSrcsList.Valid() {
|
|
|
|
deps = append(deps, commonSrcsList.Path())
|
|
|
|
commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
|
|
|
|
}
|
2019-01-18 00:42:52 +01:00
|
|
|
|
2019-10-31 23:22:57 +01:00
|
|
|
kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=")
|
2019-01-18 00:42:52 +01:00
|
|
|
|
2019-01-22 06:37:16 +01:00
|
|
|
kaptProcessor := ""
|
2020-04-22 20:44:34 +02:00
|
|
|
for i, p := range flags.processors {
|
|
|
|
if i > 0 {
|
|
|
|
kaptProcessor += " "
|
|
|
|
}
|
|
|
|
kaptProcessor += "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + p
|
2019-01-22 06:37:16 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 00:42:52 +01:00
|
|
|
encodedJavacFlags := kaptEncodeFlags([][2]string{
|
2019-10-28 19:37:20 +01:00
|
|
|
{"-source", flags.javaVersion.String()},
|
|
|
|
{"-target", flags.javaVersion.String()},
|
2019-01-18 00:42:52 +01:00
|
|
|
})
|
|
|
|
|
2019-05-08 23:30:12 +02:00
|
|
|
kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
|
|
|
|
kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
|
|
|
|
|
2022-03-17 02:06:48 +01:00
|
|
|
// First run kapt to generate .java stubs from .kt files
|
|
|
|
kaptStubsJar := android.PathForModuleOut(ctx, "kapt", "stubs.jar")
|
2019-01-18 00:42:52 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2022-03-17 02:06:48 +01:00
|
|
|
Rule: kaptStubs,
|
|
|
|
Description: "kapt stubs",
|
|
|
|
Output: kaptStubsJar,
|
|
|
|
Inputs: srcFiles,
|
|
|
|
Implicits: deps,
|
2019-01-18 00:42:52 +01:00
|
|
|
Args: map[string]string{
|
2020-06-26 02:12:28 +02:00
|
|
|
"classpath": flags.kotlincClasspath.FormJavaClassPath(""),
|
2019-01-18 00:42:52 +01:00
|
|
|
"kotlincFlags": flags.kotlincFlags,
|
2020-06-26 02:13:36 +02:00
|
|
|
"commonSrcFilesArg": commonSrcFilesArg,
|
2019-01-18 00:42:52 +01:00
|
|
|
"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 06:37:16 +01:00
|
|
|
"kaptProcessor": kaptProcessor,
|
2019-01-18 00:42:52 +01:00
|
|
|
"kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(),
|
|
|
|
"encodedJavacFlags": encodedJavacFlags,
|
2019-05-08 23:30:12 +02:00
|
|
|
"name": kotlinName,
|
2020-06-19 00:46:32 +02:00
|
|
|
"classesJarOut": resJarOutputFile.String(),
|
2019-01-18 00:42:52 +01:00
|
|
|
},
|
|
|
|
})
|
2022-03-17 02:06:48 +01:00
|
|
|
|
|
|
|
// Then run turbine to perform annotation processing on the stubs and any .java srcFiles.
|
|
|
|
javaSrcFiles := srcFiles.FilterByExt(".java")
|
|
|
|
turbineSrcJars := append(android.Paths{kaptStubsJar}, srcJars...)
|
2022-04-06 08:14:38 +02:00
|
|
|
TurbineApt(ctx, srcJarOutputFile, resJarOutputFile, javaSrcFiles, turbineSrcJars, flags)
|
2019-01-18 00:42:52 +01: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()...))
|
|
|
|
}
|