Merge "Support kotlin multiplatform sources"
This commit is contained in:
17
java/java.go
17
java/java.go
@@ -140,10 +140,15 @@ func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
|
|||||||
// Findbugs
|
// Findbugs
|
||||||
|
|
||||||
type CompilerProperties struct {
|
type CompilerProperties struct {
|
||||||
// list of source files used to compile the Java module. May be .java, .logtags, .proto,
|
// list of source files used to compile the Java module. May be .java, .kt, .logtags, .proto,
|
||||||
// or .aidl files.
|
// or .aidl files.
|
||||||
Srcs []string `android:"path,arch_variant"`
|
Srcs []string `android:"path,arch_variant"`
|
||||||
|
|
||||||
|
// list Kotlin of source files containing Kotlin code that should be treated as common code in
|
||||||
|
// a codebase that supports Kotlin multiplatform. See
|
||||||
|
// https://kotlinlang.org/docs/reference/multiplatform.html. May be only be .kt files.
|
||||||
|
Common_srcs []string `android:"path,arch_variant"`
|
||||||
|
|
||||||
// list of source files that should not be used to build the Java module.
|
// list of source files that should not be used to build the Java module.
|
||||||
// This is most useful in the arch/multilib variants to remove non-common files
|
// This is most useful in the arch/multilib variants to remove non-common files
|
||||||
Exclude_srcs []string `android:"path,arch_variant"`
|
Exclude_srcs []string `android:"path,arch_variant"`
|
||||||
@@ -1300,6 +1305,11 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
|
|||||||
flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
|
flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kotlinCommonSrcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Common_srcs, nil)
|
||||||
|
if len(kotlinCommonSrcFiles.FilterOutByExt(".kt")) > 0 {
|
||||||
|
ctx.PropertyErrorf("common_srcs", "common_srcs must be .kt files")
|
||||||
|
}
|
||||||
|
|
||||||
srcFiles = j.genSources(ctx, srcFiles, flags)
|
srcFiles = j.genSources(ctx, srcFiles, flags)
|
||||||
|
|
||||||
srcJars := srcFiles.FilterByExt(".srcjar")
|
srcJars := srcFiles.FilterByExt(".srcjar")
|
||||||
@@ -1353,6 +1363,7 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
|
|||||||
|
|
||||||
// Collect .kt files for AIDEGen
|
// Collect .kt files for AIDEGen
|
||||||
j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
|
j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
|
||||||
|
j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, kotlinCommonSrcFiles.Strings()...)
|
||||||
|
|
||||||
flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
|
flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
|
||||||
flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
|
flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
|
||||||
@@ -1364,7 +1375,7 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
|
|||||||
// Use kapt for annotation processing
|
// Use kapt for annotation processing
|
||||||
kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
|
kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
|
||||||
kaptResJar := android.PathForModuleOut(ctx, "kapt", "kapt-res.jar")
|
kaptResJar := android.PathForModuleOut(ctx, "kapt", "kapt-res.jar")
|
||||||
kotlinKapt(ctx, kaptSrcJar, kaptResJar, kotlinSrcFiles, srcJars, flags)
|
kotlinKapt(ctx, kaptSrcJar, kaptResJar, kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
|
||||||
srcJars = append(srcJars, kaptSrcJar)
|
srcJars = append(srcJars, kaptSrcJar)
|
||||||
kotlinJars = append(kotlinJars, kaptResJar)
|
kotlinJars = append(kotlinJars, kaptResJar)
|
||||||
// Disable annotation processing in javac, it's already been handled by kapt
|
// Disable annotation processing in javac, it's already been handled by kapt
|
||||||
@@ -1373,7 +1384,7 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
|
kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
|
||||||
kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
|
kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
|
||||||
if ctx.Failed() {
|
if ctx.Failed() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@@ -33,7 +33,7 @@ var kotlinc = pctx.AndroidRemoteStaticRule("kotlinc", android.RemoteRuleSupports
|
|||||||
`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
|
`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
|
||||||
`${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
|
`${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
|
||||||
` --out_dir "$classesDir" --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
|
` --out_dir "$classesDir" --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
|
||||||
` --out "$kotlinBuildFile" && ` +
|
` $commonSrcFilesArg --out "$kotlinBuildFile" && ` +
|
||||||
`${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` +
|
`${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` +
|
||||||
`-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile -kotlin-home $emptyDir && ` +
|
`-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile -kotlin-home $emptyDir && ` +
|
||||||
`${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` +
|
`${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` +
|
||||||
@@ -54,21 +54,43 @@ var kotlinc = pctx.AndroidRemoteStaticRule("kotlinc", android.RemoteRuleSupports
|
|||||||
Rspfile: "$out.rsp",
|
Rspfile: "$out.rsp",
|
||||||
RspfileContent: `$in`,
|
RspfileContent: `$in`,
|
||||||
},
|
},
|
||||||
"kotlincFlags", "classpath", "srcJars", "srcJarDir", "classesDir", "kotlinJvmTarget", "kotlinBuildFile",
|
"kotlincFlags", "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "classesDir",
|
||||||
"emptyDir", "name")
|
"kotlinJvmTarget", "kotlinBuildFile", "emptyDir", "name")
|
||||||
|
|
||||||
|
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")
|
||||||
|
rule := android.NewRuleBuilder()
|
||||||
|
rule.Command().Text("cp").FlagWithRspFileInputList("", commonSrcFiles).Output(commonSrcsList)
|
||||||
|
rule.Build(pctx, ctx, "kotlin_common_srcs_list", "kotlin common_srcs list")
|
||||||
|
return android.OptionalPathForPath(commonSrcsList)
|
||||||
|
}
|
||||||
|
return android.OptionalPath{}
|
||||||
|
}
|
||||||
|
|
||||||
// kotlinCompile takes .java and .kt sources and srcJars, and compiles the .kt sources into a classes jar in outputFile.
|
// 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,
|
func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath,
|
||||||
srcFiles, srcJars android.Paths,
|
srcFiles, commonSrcFiles, srcJars android.Paths,
|
||||||
flags javaBuilderFlags) {
|
flags javaBuilderFlags) {
|
||||||
|
|
||||||
var deps android.Paths
|
var deps android.Paths
|
||||||
deps = append(deps, flags.kotlincClasspath...)
|
deps = append(deps, flags.kotlincClasspath...)
|
||||||
deps = append(deps, srcJars...)
|
deps = append(deps, srcJars...)
|
||||||
|
deps = append(deps, commonSrcFiles...)
|
||||||
|
|
||||||
kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
|
kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
|
||||||
kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
|
kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
|
||||||
|
|
||||||
|
commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
|
||||||
|
commonSrcFilesArg := ""
|
||||||
|
if commonSrcsList.Valid() {
|
||||||
|
deps = append(deps, commonSrcsList.Path())
|
||||||
|
commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
|
||||||
|
}
|
||||||
|
|
||||||
ctx.Build(pctx, android.BuildParams{
|
ctx.Build(pctx, android.BuildParams{
|
||||||
Rule: kotlinc,
|
Rule: kotlinc,
|
||||||
Description: "kotlinc",
|
Description: "kotlinc",
|
||||||
@@ -76,13 +98,14 @@ func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath,
|
|||||||
Inputs: srcFiles,
|
Inputs: srcFiles,
|
||||||
Implicits: deps,
|
Implicits: deps,
|
||||||
Args: map[string]string{
|
Args: map[string]string{
|
||||||
"classpath": flags.kotlincClasspath.FormJavaClassPath(""),
|
"classpath": flags.kotlincClasspath.FormJavaClassPath(""),
|
||||||
"kotlincFlags": flags.kotlincFlags,
|
"kotlincFlags": flags.kotlincFlags,
|
||||||
"srcJars": strings.Join(srcJars.Strings(), " "),
|
"commonSrcFilesArg": commonSrcFilesArg,
|
||||||
"classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
|
"srcJars": strings.Join(srcJars.Strings(), " "),
|
||||||
"srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
|
"classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
|
||||||
"kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(),
|
"srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
|
||||||
"emptyDir": android.PathForModuleOut(ctx, "kotlinc", "empty").String(),
|
"kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(),
|
||||||
|
"emptyDir": android.PathForModuleOut(ctx, "kotlinc", "empty").String(),
|
||||||
// http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
|
// http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
|
||||||
"kotlinJvmTarget": "1.8",
|
"kotlinJvmTarget": "1.8",
|
||||||
"name": kotlinName,
|
"name": kotlinName,
|
||||||
@@ -97,7 +120,7 @@ var kapt = pctx.AndroidRemoteStaticRule("kapt", android.RemoteRuleSupports{Goma:
|
|||||||
`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
|
`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
|
||||||
`${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
|
`${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
|
||||||
` --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
|
` --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
|
||||||
` --out "$kotlinBuildFile" && ` +
|
` $commonSrcFilesArg --out "$kotlinBuildFile" && ` +
|
||||||
`${config.KotlincCmd} ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} $kotlincFlags ` +
|
`${config.KotlincCmd} ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} $kotlincFlags ` +
|
||||||
`-Xplugin=${config.KotlinKaptJar} ` +
|
`-Xplugin=${config.KotlinKaptJar} ` +
|
||||||
`-P plugin:org.jetbrains.kotlin.kapt3:sources=$kaptDir/sources ` +
|
`-P plugin:org.jetbrains.kotlin.kapt3:sources=$kaptDir/sources ` +
|
||||||
@@ -124,21 +147,31 @@ var kapt = pctx.AndroidRemoteStaticRule("kapt", android.RemoteRuleSupports{Goma:
|
|||||||
RspfileContent: `$in`,
|
RspfileContent: `$in`,
|
||||||
},
|
},
|
||||||
"kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor",
|
"kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor",
|
||||||
"classpath", "srcJars", "srcJarDir", "kaptDir", "kotlinJvmTarget", "kotlinBuildFile", "name",
|
"classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "kaptDir", "kotlinJvmTarget",
|
||||||
"classesJarOut")
|
"kotlinBuildFile", "name", "classesJarOut")
|
||||||
|
|
||||||
// kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs
|
// 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
|
// 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
|
// added as an additional input to kotlinc and javac rules, and the javac rule should have annotation processing
|
||||||
// disabled.
|
// disabled.
|
||||||
func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile android.WritablePath,
|
func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile android.WritablePath,
|
||||||
srcFiles, srcJars android.Paths,
|
srcFiles, commonSrcFiles, srcJars android.Paths,
|
||||||
flags javaBuilderFlags) {
|
flags javaBuilderFlags) {
|
||||||
|
|
||||||
|
srcFiles = append(android.Paths(nil), srcFiles...)
|
||||||
|
|
||||||
var deps android.Paths
|
var deps android.Paths
|
||||||
deps = append(deps, flags.kotlincClasspath...)
|
deps = append(deps, flags.kotlincClasspath...)
|
||||||
deps = append(deps, srcJars...)
|
deps = append(deps, srcJars...)
|
||||||
deps = append(deps, flags.processorPath...)
|
deps = append(deps, flags.processorPath...)
|
||||||
|
deps = append(deps, commonSrcFiles...)
|
||||||
|
|
||||||
|
commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
|
||||||
|
commonSrcFilesArg := ""
|
||||||
|
if commonSrcsList.Valid() {
|
||||||
|
deps = append(deps, commonSrcsList.Path())
|
||||||
|
commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
|
||||||
|
}
|
||||||
|
|
||||||
kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=")
|
kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=")
|
||||||
|
|
||||||
@@ -168,6 +201,7 @@ func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile an
|
|||||||
Args: map[string]string{
|
Args: map[string]string{
|
||||||
"classpath": flags.kotlincClasspath.FormJavaClassPath(""),
|
"classpath": flags.kotlincClasspath.FormJavaClassPath(""),
|
||||||
"kotlincFlags": flags.kotlincFlags,
|
"kotlincFlags": flags.kotlincFlags,
|
||||||
|
"commonSrcFilesArg": commonSrcFilesArg,
|
||||||
"srcJars": strings.Join(srcJars.Strings(), " "),
|
"srcJars": strings.Join(srcJars.Strings(), " "),
|
||||||
"srcJarDir": android.PathForModuleOut(ctx, "kapt", "srcJars").String(),
|
"srcJarDir": android.PathForModuleOut(ctx, "kapt", "srcJars").String(),
|
||||||
"kotlinBuildFile": android.PathForModuleOut(ctx, "kapt", "build.xml").String(),
|
"kotlinBuildFile": android.PathForModuleOut(ctx, "kapt", "build.xml").String(),
|
||||||
|
Reference in New Issue
Block a user