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:
@@ -16,13 +16,21 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.ant;
|
package org.jetbrains.kotlin.ant;
|
||||||
|
|
||||||
|
import kotlin.KotlinPackage;
|
||||||
|
import kotlin.jvm.functions.Function1;
|
||||||
import org.apache.tools.ant.BuildException;
|
import org.apache.tools.ant.BuildException;
|
||||||
import org.apache.tools.ant.taskdefs.Javac;
|
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.taskdefs.compilers.Javac13;
|
||||||
import org.apache.tools.ant.types.Path;
|
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;
|
private Path externalAnnotations;
|
||||||
|
|
||||||
public void setExternalAnnotations(Path externalAnnotations) {
|
public void setExternalAnnotations(Path externalAnnotations) {
|
||||||
@@ -36,27 +44,53 @@ public class KotlinCompilerAdapter extends DefaultCompilerAdapter {
|
|||||||
return externalAnnotations.createPath();
|
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
|
@Override
|
||||||
public boolean execute() throws BuildException {
|
public boolean execute() throws BuildException {
|
||||||
Javac javac = getJavac();
|
Javac javac = getJavac();
|
||||||
|
|
||||||
Kotlin2JvmTask kotlinTask = new Kotlin2JvmTask();
|
Kotlin2JvmTask kotlinc = new Kotlin2JvmTask();
|
||||||
kotlinTask.setOutput(javac.getDestdir());
|
kotlinc.setOutput(javac.getDestdir());
|
||||||
|
|
||||||
Path classpath = javac.getClasspath();
|
Path classpath = javac.getClasspath();
|
||||||
if (classpath != null) {
|
if (classpath != null) {
|
||||||
kotlinTask.setClasspath(classpath);
|
kotlinc.setClasspath(classpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlinTask.setSrc(javac.getSrcdir());
|
// We use the provided src dir instead of compileList, because the latter is insane:
|
||||||
kotlinTask.setExternalAnnotations(externalAnnotations);
|
// 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...");
|
javac.log("Running javac...");
|
||||||
|
|
||||||
Javac13 javac13 = new Javac13();
|
// Javac13#execute passes everything in compileList to javac, which doesn't recognize .kt files
|
||||||
javac13.setJavac(javac);
|
compileList = filterOutKotlinSources(compileList);
|
||||||
return javac13.execute();
|
|
||||||
|
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()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ Buildfile: [TestData]/build.xml
|
|||||||
|
|
||||||
build:
|
build:
|
||||||
[mkdir] Created dir: [Temp]/classes
|
[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] Compiling [[TestData]/root1] => [[Temp]/classes]
|
||||||
[javac] Running javac...
|
[javac] Running javac...
|
||||||
[jar] Building jar: [Temp]/hello.jar
|
[jar] Building jar: [Temp]/hello.jar
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ Buildfile: [TestData]/build.xml
|
|||||||
|
|
||||||
build:
|
build:
|
||||||
[mkdir] Created dir: [Temp]/classes
|
[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] Compiling [[TestData]/root1] => [[Temp]/classes]
|
||||||
[javac] Running javac...
|
[javac] Running javac...
|
||||||
[jar] Building jar: [Temp]/hello.jar
|
[jar] Building jar: [Temp]/hello.jar
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ Buildfile: [TestData]/build.xml
|
|||||||
|
|
||||||
build:
|
build:
|
||||||
[mkdir] Created dir: [Temp]/classes
|
[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] Compiling [[TestData]] => [[Temp]/classes]
|
||||||
[javac] Running javac...
|
[javac] Running javac...
|
||||||
[jar] Building jar: [Temp]/hello.jar
|
[jar] Building jar: [Temp]/hello.jar
|
||||||
|
|||||||
+14
@@ -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!")
|
||||||
|
}
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
OUT:
|
||||||
|
Hello!
|
||||||
|
|
||||||
|
Return code: 0
|
||||||
@@ -101,6 +101,11 @@ public class AntTaskJvmTest extends AntTaskBaseTest {
|
|||||||
doJvmAntTest();
|
doJvmAntTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void withKotlinNoJavaSources() throws Exception {
|
||||||
|
doJvmAntTest();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void javacCompiler() throws Exception {
|
public void javacCompiler() throws Exception {
|
||||||
doJvmAntTest("-cp", getClassPathForAnt(),
|
doJvmAntTest("-cp", getClassPathForAnt(),
|
||||||
|
|||||||
Reference in New Issue
Block a user