Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4b5fe9d1 authored by Przemyslaw Szczepaniak's avatar Przemyslaw Szczepaniak Committed by Colin Cross
Browse files

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
parent 51be632b
Loading
Loading
Loading
Loading
+14 −10
Original line number Diff line number Diff line
@@ -61,19 +61,23 @@ var (

	kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
		blueprint.RuleParams{
			// TODO(ccross): kotlinc doesn't support @ file for arguments, which will limit the
			// maximum number of input files, especially on darwin.
			Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
				`${config.KotlincCmd} $classpath $kotlincFlags ` +
				`-jvm-target $kotlinJvmTarget -d $outDir $in && ` +
			Command: `rm -rf "$outDir" "$srcJarDir" && mkdir -p "$outDir" "$srcJarDir" && ` +
				`${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
				`${config.GenKotlinBuildFileCmd} $classpath $outDir $out.rsp $srcJarDir/list > $outDir/kotlinc-build.xml &&` +
				`${config.KotlincCmd} $kotlincFlags ` +
				`-jvm-target $kotlinJvmTarget -Xbuild-file=$outDir/kotlinc-build.xml && ` +
				`${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
			CommandDeps: []string{
				"${config.KotlincCmd}",
				"${config.KotlinCompilerJar}",
				"${config.GenKotlinBuildFileCmd}",
				"${config.SoongZipCmd}",
				"${config.ExtractSrcJarsCmd}",
			},
			Rspfile:        "$out.rsp",
			RspfileContent: `$in`,
		},
		"kotlincFlags", "classpath", "outDir", "kotlinJvmTarget")
		"kotlincFlags", "classpath", "srcJars", "srcJarDir", "outDir", "kotlinJvmTarget")

	errorprone = pctx.AndroidStaticRule("errorprone",
		blueprint.RuleParams{
@@ -171,13 +175,11 @@ func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.Writ
	srcFiles, srcJars android.Paths,
	flags javaBuilderFlags) {

	classDir := android.PathForModuleOut(ctx, "kotlinc", "classes")

	inputs := append(android.Paths(nil), srcFiles...)
	inputs = append(inputs, srcJars...)

	var deps android.Paths
	deps = append(deps, flags.kotlincClasspath...)
	deps = append(deps, srcJars...)

	ctx.Build(pctx, android.BuildParams{
		Rule:        kotlinc,
@@ -188,7 +190,9 @@ func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.Writ
		Args: map[string]string{
			"classpath":    flags.kotlincClasspath.FormJavaClassPath("-classpath"),
			"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
			"kotlinJvmTarget": "1.8",
		},
+2 −0
Original line number Diff line number Diff line
@@ -83,6 +83,8 @@ func init() {
	pctx.SourcePathVariable("Ziptime", "prebuilts/build-tools/${hostPrebuiltTag}/bin/ziptime")

	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.HostBinToolVariable("SoongZipCmd", "soong_zip")
	pctx.HostBinToolVariable("MergeZipsCmd", "merge_zips")
+15 −11
Original line number Diff line number Diff line
@@ -757,6 +757,16 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path

	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 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
@@ -767,11 +777,15 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
			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.classpath...)

		kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
		TransformKotlinToClasses(ctx, kotlinJar, srcFiles, srcJars, flags)
		TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
		if ctx.Failed() {
			return
		}
@@ -783,16 +797,6 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
		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
	j.compiledJavaSrcs = uniqueSrcFiles
	j.compiledSrcJars = srcJars
+60 −0
Original line number Diff line number Diff line
#!/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>"