Add rsp and srcjar support to kotlinc build rule

Rsp files are supported through helper script (gen-kotlin-build-file.sh)
that generates the kotlinc module/build xml file.

Since rsp files are supported, I've added ExtractSrcJarsCmd step
to handle srcjars extraction.

Minor reorderings to make sure that TransformKotlinToClasses
recives only .java and .kt files when called from Module.compile.

Bug: 73281388
Test: make -j hidl-doc
Change-Id: I5a40b914569018dc529903a7f2864a5aeae838e5
This commit is contained in:
Przemyslaw Szczepaniak 2018-02-13 14:32:54 +00:00 committed by Colin Cross
parent 51be632b95
commit 4b5fe9d1b4
4 changed files with 91 additions and 21 deletions

View File

@ -61,19 +61,23 @@ var (
kotlinc = pctx.AndroidGomaStaticRule("kotlinc", kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
blueprint.RuleParams{ blueprint.RuleParams{
// TODO(ccross): kotlinc doesn't support @ file for arguments, which will limit the Command: `rm -rf "$outDir" "$srcJarDir" && mkdir -p "$outDir" "$srcJarDir" && ` +
// maximum number of input files, especially on darwin. `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + `${config.GenKotlinBuildFileCmd} $classpath $outDir $out.rsp $srcJarDir/list > $outDir/kotlinc-build.xml &&` +
`${config.KotlincCmd} $classpath $kotlincFlags ` + `${config.KotlincCmd} $kotlincFlags ` +
`-jvm-target $kotlinJvmTarget -d $outDir $in && ` + `-jvm-target $kotlinJvmTarget -Xbuild-file=$outDir/kotlinc-build.xml && ` +
`${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`, `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
CommandDeps: []string{ CommandDeps: []string{
"${config.KotlincCmd}", "${config.KotlincCmd}",
"${config.KotlinCompilerJar}", "${config.KotlinCompilerJar}",
"${config.GenKotlinBuildFileCmd}",
"${config.SoongZipCmd}", "${config.SoongZipCmd}",
"${config.ExtractSrcJarsCmd}",
}, },
Rspfile: "$out.rsp",
RspfileContent: `$in`,
}, },
"kotlincFlags", "classpath", "outDir", "kotlinJvmTarget") "kotlincFlags", "classpath", "srcJars", "srcJarDir", "outDir", "kotlinJvmTarget")
errorprone = pctx.AndroidStaticRule("errorprone", errorprone = pctx.AndroidStaticRule("errorprone",
blueprint.RuleParams{ blueprint.RuleParams{
@ -171,13 +175,11 @@ func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.Writ
srcFiles, srcJars android.Paths, srcFiles, srcJars android.Paths,
flags javaBuilderFlags) { flags javaBuilderFlags) {
classDir := android.PathForModuleOut(ctx, "kotlinc", "classes")
inputs := append(android.Paths(nil), srcFiles...) inputs := append(android.Paths(nil), srcFiles...)
inputs = append(inputs, srcJars...)
var deps android.Paths var deps android.Paths
deps = append(deps, flags.kotlincClasspath...) deps = append(deps, flags.kotlincClasspath...)
deps = append(deps, srcJars...)
ctx.Build(pctx, android.BuildParams{ ctx.Build(pctx, android.BuildParams{
Rule: kotlinc, Rule: kotlinc,
@ -188,7 +190,9 @@ func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.Writ
Args: map[string]string{ Args: map[string]string{
"classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"), "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
"kotlincFlags": flags.kotlincFlags, "kotlincFlags": flags.kotlincFlags,
"outDir": classDir.String(), "srcJars": strings.Join(srcJars.Strings(), " "),
"outDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
"srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").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",
}, },

View File

@ -83,6 +83,8 @@ func init() {
pctx.SourcePathVariable("Ziptime", "prebuilts/build-tools/${hostPrebuiltTag}/bin/ziptime") pctx.SourcePathVariable("Ziptime", "prebuilts/build-tools/${hostPrebuiltTag}/bin/ziptime")
pctx.SourcePathVariable("ExtractSrcJarsCmd", "build/soong/scripts/extract-srcjars.sh") pctx.SourcePathVariable("ExtractSrcJarsCmd", "build/soong/scripts/extract-srcjars.sh")
pctx.SourcePathVariable("GenKotlinBuildFileCmd", "build/soong/scripts/gen-kotlin-build-file.sh")
pctx.SourcePathVariable("JarArgsCmd", "build/soong/scripts/jar-args.sh") pctx.SourcePathVariable("JarArgsCmd", "build/soong/scripts/jar-args.sh")
pctx.HostBinToolVariable("SoongZipCmd", "soong_zip") pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
pctx.HostBinToolVariable("MergeZipsCmd", "merge_zips") pctx.HostBinToolVariable("MergeZipsCmd", "merge_zips")

View File

@ -757,6 +757,16 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
jarName := ctx.ModuleName() + ".jar" jarName := ctx.ModuleName() + ".jar"
javaSrcFiles := srcFiles.FilterByExt(".java")
var uniqueSrcFiles android.Paths
set := make(map[string]bool)
for _, v := range javaSrcFiles {
if _, found := set[v.String()]; !found {
set[v.String()] = true
uniqueSrcFiles = append(uniqueSrcFiles, v)
}
}
if srcFiles.HasExt(".kt") { if srcFiles.HasExt(".kt") {
// If there are kotlin files, compile them first but pass all the kotlin and java files // If there are kotlin files, compile them first but pass all the kotlin and java files
// kotlinc will use the java files to resolve types referenced by the kotlin files, but // kotlinc will use the java files to resolve types referenced by the kotlin files, but
@ -767,11 +777,15 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
flags.kotlincFlags += " -no-jdk" flags.kotlincFlags += " -no-jdk"
} }
var kotlinSrcFiles android.Paths
kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...) flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...) flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName) kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
TransformKotlinToClasses(ctx, kotlinJar, srcFiles, srcJars, flags) TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
if ctx.Failed() { if ctx.Failed() {
return return
} }
@ -783,16 +797,6 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
jars = append(jars, deps.kotlinStdlib...) jars = append(jars, deps.kotlinStdlib...)
} }
javaSrcFiles := srcFiles.FilterByExt(".java")
var uniqueSrcFiles android.Paths
set := make(map[string]bool)
for _, v := range javaSrcFiles {
if _, found := set[v.String()]; !found {
set[v.String()] = true
uniqueSrcFiles = append(uniqueSrcFiles, v)
}
}
// Store the list of .java files that was passed to javac // Store the list of .java files that was passed to javac
j.compiledJavaSrcs = uniqueSrcFiles j.compiledJavaSrcs = uniqueSrcFiles
j.compiledSrcJars = srcJars j.compiledSrcJars = srcJars

View File

@ -0,0 +1,60 @@
#!/bin/bash -e
# Copyright 2018 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.
# Generates kotlinc module xml file to standard output based on rsp files
if [ -z "$1" ]; then
echo "usage: $0 <classpath> <outDir> <rspFiles>..." >&2
exit 1
fi
# Classpath variable has a tendency to be prefixed by "-classpath", remove it.
if [[ $1 == "-classpath" ]]; then
shift
fi;
classpath=$1
out_dir=$2
shift 2
# Path in the build file are relative to the build file, we need to make them absolute.
prefix=`pwd`
# Print preamble
echo "<modules><module name=\"name\" type=\"java-production\" outputDir=\"${out_dir}\">"
# Print classpath entries
for file in $(echo $classpath | tr ":" "\n"); do
echo " <classpath path=\"${prefix}/${file}\"/>"
done
# For each rsp file, print source entries
while (( "$#" )); do
for file in $(cat $1); do
if [[ $file == *.java ]]; then
echo " <javaSourceRoots path=\"${prefix}/${file}\"/>"
elif [[ $file == *.kt ]]; then
echo " <sources path=\"${prefix}/${file}\"/>"
else
echo "Unknown source file type ${file}"
exit 1
fi
done
shift
done
echo "</module></modules>"