Always run kotlinc in <withKotlin/> Ant task

This change requires withKotlin to be run with Ant of version at least 1.8.2.

Some test data changed because now compileList contains both Java and Kotlin
sources

 #KT-7870 Fixed
This commit is contained in:
Alexander Udalov
2015-06-02 16:14:38 +03:00
parent cbbf8dec7e
commit 6d00c265e2
9 changed files with 89 additions and 14 deletions
@@ -16,13 +16,21 @@
package org.jetbrains.kotlin.ant;
import kotlin.KotlinPackage;
import kotlin.jvm.functions.Function1;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter;
import org.apache.tools.ant.taskdefs.compilers.Javac13;
import org.apache.tools.ant.types.Path;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.Arrays;
import java.util.List;
public class KotlinCompilerAdapter extends Javac13 {
private static final List<String> KOTLIN_EXTENSIONS = Arrays.asList("kt", "kts");
public class KotlinCompilerAdapter extends DefaultCompilerAdapter {
private Path externalAnnotations;
public void setExternalAnnotations(Path externalAnnotations) {
@@ -36,27 +44,53 @@ public class KotlinCompilerAdapter extends DefaultCompilerAdapter {
return externalAnnotations.createPath();
}
@Override
public String[] getSupportedFileExtensions() {
List<String> result = KotlinPackage.plus(Arrays.asList(super.getSupportedFileExtensions()), KOTLIN_EXTENSIONS);
//noinspection SSBasedInspection
return result.toArray(new String[result.size()]);
}
@Override
public boolean execute() throws BuildException {
Javac javac = getJavac();
Kotlin2JvmTask kotlinTask = new Kotlin2JvmTask();
kotlinTask.setOutput(javac.getDestdir());
Kotlin2JvmTask kotlinc = new Kotlin2JvmTask();
kotlinc.setOutput(javac.getDestdir());
Path classpath = javac.getClasspath();
if (classpath != null) {
kotlinTask.setClasspath(classpath);
kotlinc.setClasspath(classpath);
}
kotlinTask.setSrc(javac.getSrcdir());
kotlinTask.setExternalAnnotations(externalAnnotations);
// We use the provided src dir instead of compileList, because the latter is insane:
// it is constructed only of sources which are newer than classes with the same name
kotlinc.setSrc(javac.getSrcdir());
kotlinTask.execute();
kotlinc.setExternalAnnotations(externalAnnotations);
kotlinc.execute();
javac.log("Running javac...");
Javac13 javac13 = new Javac13();
javac13.setJavac(javac);
return javac13.execute();
// Javac13#execute passes everything in compileList to javac, which doesn't recognize .kt files
compileList = filterOutKotlinSources(compileList);
return compileList.length == 0 || super.execute();
}
@NotNull
private static File[] filterOutKotlinSources(@NotNull File[] files) {
List<File> nonKotlinSources = KotlinPackage.filterNot(files, new Function1<File, Boolean>() {
@Override
public Boolean invoke(File file) {
for (String extension : KOTLIN_EXTENSIONS) {
if (file.getPath().endsWith("." + extension)) return true;
}
return false;
}
});
return nonKotlinSources.toArray(new File[nonKotlinSources.size()]);
}
}
@@ -3,7 +3,7 @@ Buildfile: [TestData]/build.xml
build:
[mkdir] Created dir: [Temp]/classes
[javac] Compiling 1 source file to [Temp]/classes
[javac] Compiling 2 source files to [Temp]/classes
[javac] Compiling [[TestData]/root1] => [[Temp]/classes]
[javac] Running javac...
[jar] Building jar: [Temp]/hello.jar
@@ -3,7 +3,7 @@ Buildfile: [TestData]/build.xml
build:
[mkdir] Created dir: [Temp]/classes
[javac] Compiling 1 source file to [Temp]/classes
[javac] Compiling 2 source files to [Temp]/classes
[javac] Compiling [[TestData]/root1] => [[Temp]/classes]
[javac] Running javac...
[jar] Building jar: [Temp]/hello.jar
@@ -3,7 +3,7 @@ Buildfile: [TestData]/build.xml
build:
[mkdir] Created dir: [Temp]/classes
[javac] Compiling 1 source file to [Temp]/classes
[javac] Compiling 2 source files to [Temp]/classes
[javac] Compiling [[TestData]] => [[Temp]/classes]
[javac] Running javac...
[jar] Building jar: [Temp]/hello.jar
@@ -0,0 +1,14 @@
OUT:
Buildfile: [TestData]/build.xml
build:
[mkdir] Created dir: [Temp]/classes
[javac] Compiling 1 source file to [Temp]/classes
[javac] Compiling [[TestData]] => [[Temp]/classes]
[javac] Running javac...
[jar] Building jar: [Temp]/hello.jar
BUILD SUCCESSFUL
Total time: [time]
Return code: 0
@@ -0,0 +1,13 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/kotlin/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<mkdir dir="${temp}/classes"/>
<javac srcdir="${test.data}" destdir="${temp}/classes" includeantruntime="false">
<withKotlin/>
</javac>
<jar destfile="${temp}/hello.jar">
<fileset dir="${temp}/classes"/>
</jar>
</target>
</project>
@@ -0,0 +1,5 @@
package hello
fun main(args: Array<String>) {
println("Hello!")
}
@@ -0,0 +1,4 @@
OUT:
Hello!
Return code: 0
@@ -101,6 +101,11 @@ public class AntTaskJvmTest extends AntTaskBaseTest {
doJvmAntTest();
}
@Test
public void withKotlinNoJavaSources() throws Exception {
doJvmAntTest();
}
@Test
public void javacCompiler() throws Exception {
doJvmAntTest("-cp", getClassPathForAnt(),