diff --git a/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.java b/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.java index 5017309c7b5..e3b941b1cc6 100644 --- a/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.java +++ b/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.java @@ -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 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 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 nonKotlinSources = KotlinPackage.filterNot(files, new Function1() { + @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()]); } } diff --git a/compiler/testData/integration/ant/jvm/externalAnnotations/build.log.expected b/compiler/testData/integration/ant/jvm/externalAnnotations/build.log.expected index 1ee5576831e..b0482150f40 100644 --- a/compiler/testData/integration/ant/jvm/externalAnnotations/build.log.expected +++ b/compiler/testData/integration/ant/jvm/externalAnnotations/build.log.expected @@ -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 diff --git a/compiler/testData/integration/ant/jvm/javacCompiler/build.log.expected b/compiler/testData/integration/ant/jvm/javacCompiler/build.log.expected index 1ee5576831e..b0482150f40 100644 --- a/compiler/testData/integration/ant/jvm/javacCompiler/build.log.expected +++ b/compiler/testData/integration/ant/jvm/javacCompiler/build.log.expected @@ -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 diff --git a/compiler/testData/integration/ant/jvm/noClasspathGiven/build.log.expected b/compiler/testData/integration/ant/jvm/noClasspathGiven/build.log.expected index 43571e1ce89..32882961591 100644 --- a/compiler/testData/integration/ant/jvm/noClasspathGiven/build.log.expected +++ b/compiler/testData/integration/ant/jvm/noClasspathGiven/build.log.expected @@ -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 diff --git a/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/build.log.expected b/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/build.log.expected new file mode 100644 index 00000000000..43571e1ce89 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/build.log.expected @@ -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 diff --git a/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/build.xml b/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/build.xml new file mode 100644 index 00000000000..3b3c222aab9 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/build.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/hello.kt b/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/hello.kt new file mode 100644 index 00000000000..6c9ab7fe60a --- /dev/null +++ b/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/hello.kt @@ -0,0 +1,5 @@ +package hello + +fun main(args: Array) { + println("Hello!") +} diff --git a/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/hello.run.expected b/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/hello.run.expected new file mode 100644 index 00000000000..fd58d467c30 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/hello.run.expected @@ -0,0 +1,4 @@ +OUT: +Hello! + +Return code: 0 diff --git a/compiler/tests/org/jetbrains/kotlin/integration/AntTaskJvmTest.java b/compiler/tests/org/jetbrains/kotlin/integration/AntTaskJvmTest.java index bdbddd380c7..236db6c4946 100644 --- a/compiler/tests/org/jetbrains/kotlin/integration/AntTaskJvmTest.java +++ b/compiler/tests/org/jetbrains/kotlin/integration/AntTaskJvmTest.java @@ -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(),