Ant: Add support for fork-mode
#KT-44293
This commit is contained in:
committed by
Alexander Udalov
parent
ee952db1a2
commit
0110b4e4b4
@@ -16,9 +16,12 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.ant
|
package org.jetbrains.kotlin.ant
|
||||||
|
|
||||||
import org.apache.tools.ant.types.Path
|
import org.apache.tools.ant.BuildException
|
||||||
import org.apache.tools.ant.types.Reference
|
import org.apache.tools.ant.taskdefs.Execute
|
||||||
|
import org.apache.tools.ant.taskdefs.Redirector
|
||||||
|
import org.apache.tools.ant.types.*
|
||||||
import java.io.File.pathSeparator
|
import java.io.File.pathSeparator
|
||||||
|
import java.io.File.separator
|
||||||
|
|
||||||
class Kotlin2JvmTask : KotlinCompilerBaseTask() {
|
class Kotlin2JvmTask : KotlinCompilerBaseTask() {
|
||||||
override val compilerFqName = "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler"
|
override val compilerFqName = "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler"
|
||||||
@@ -28,6 +31,9 @@ class Kotlin2JvmTask : KotlinCompilerBaseTask() {
|
|||||||
|
|
||||||
var noReflect: Boolean = false
|
var noReflect: Boolean = false
|
||||||
|
|
||||||
|
private val cmdl = CommandlineJava()
|
||||||
|
var fork: Boolean = false
|
||||||
|
|
||||||
private var compileClasspath: Path? = null
|
private var compileClasspath: Path? = null
|
||||||
|
|
||||||
fun setClasspath(classpath: Path) {
|
fun setClasspath(classpath: Path) {
|
||||||
@@ -73,4 +79,47 @@ class Kotlin2JvmTask : KotlinCompilerBaseTask() {
|
|||||||
if (noReflect) args.add("-no-reflect")
|
if (noReflect) args.add("-no-reflect")
|
||||||
if (includeRuntime) args.add("-include-runtime")
|
if (includeRuntime) args.add("-include-runtime")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun execute() {
|
||||||
|
if (!fork)
|
||||||
|
super.execute()
|
||||||
|
else {
|
||||||
|
exec()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun exec() {
|
||||||
|
val javaHome = System.getProperty("java.home")
|
||||||
|
val javaBin = javaHome + separator + "bin" + separator + "java"
|
||||||
|
val redirector = Redirector(this)
|
||||||
|
|
||||||
|
fillArguments()
|
||||||
|
|
||||||
|
val command = ArrayList<String>()
|
||||||
|
command.add(javaBin)
|
||||||
|
command.addAll(cmdl.vmCommand.arguments) // jvm args
|
||||||
|
command.add("-Dorg.jetbrains.kotlin.cliMessageRenderer=FullPath") // same MessageRenderer as non-forking mode
|
||||||
|
command.add("-cp")
|
||||||
|
command.add(KotlinAntTaskUtil.compilerJar.canonicalPath)
|
||||||
|
command.add(compilerFqName)
|
||||||
|
command.addAll(args) // compiler args
|
||||||
|
|
||||||
|
// streamHandler: used to handle the input and output streams of the subprocess.
|
||||||
|
// watchdog: a watchdog for the subprocess or <code>null</code> to disable a timeout for the subprocess.
|
||||||
|
// TODO: support timeout for the subprocess
|
||||||
|
val exe = Execute(redirector.createHandler(), null)
|
||||||
|
exe.setAntRun(getProject())
|
||||||
|
exe.commandline = command.toTypedArray()
|
||||||
|
log("Executing command: ${command.joinToString(" ")}", LogLevel.DEBUG.level)
|
||||||
|
log("Compiling ${src!!.list().toList()} => [${output!!.canonicalPath}]")
|
||||||
|
val exitCode = exe.execute()
|
||||||
|
redirector.complete()
|
||||||
|
if (failOnError && exitCode != 0) {
|
||||||
|
throw BuildException("Compile failed; see the compiler error output for details.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createJvmarg(): Commandline.Argument {
|
||||||
|
return cmdl.createVmArgument()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ abstract class KotlinCompilerBaseTask : Task() {
|
|||||||
fillSpecificArguments()
|
fillSpecificArguments()
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun execute() {
|
override fun execute() {
|
||||||
fillArguments()
|
fillArguments()
|
||||||
|
|
||||||
val compilerClass = KotlinAntTaskUtil.getOrCreateClassLoader().loadClass(compilerFqName)
|
val compilerClass = KotlinAntTaskUtil.getOrCreateClassLoader().loadClass(compilerFqName)
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
OUT:
|
||||||
|
Buildfile: [TestData]/build.xml
|
||||||
|
|
||||||
|
premain:
|
||||||
|
[javac] Compiling 1 source file to [Temp]
|
||||||
|
[jar] Building jar: [Temp]/premain.jar
|
||||||
|
|
||||||
|
build:
|
||||||
|
[kotlinc] Compiling [[TestData]] => [[Temp]/fork.jar]
|
||||||
|
[kotlinc] -Xms64m
|
||||||
|
[kotlinc] -Xmx128m
|
||||||
|
|
||||||
|
BUILD SUCCESSFUL
|
||||||
|
Total time: [time]
|
||||||
|
|
||||||
|
Return code: 0
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<project name="Ant Task Test" default="build">
|
||||||
|
<taskdef resource="org/jetbrains/kotlin/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
|
||||||
|
|
||||||
|
<target name="premain">
|
||||||
|
<sequential>
|
||||||
|
<javac srcdir="${test.data}/premain" destdir="${temp}" includeAntRuntime="false"/>
|
||||||
|
<jar destfile="${temp}/premain.jar"
|
||||||
|
basedir="${temp}"
|
||||||
|
excludes="*.java *.jar">
|
||||||
|
<manifest>
|
||||||
|
<attribute name="Premain-Class" value="Premain"/>
|
||||||
|
</manifest>
|
||||||
|
</jar>
|
||||||
|
</sequential>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="build" depends="premain">
|
||||||
|
<kotlinc src="${test.data}" output="${temp}/fork.jar" fork="true">
|
||||||
|
<jvmarg value="-Xms64m"/>
|
||||||
|
<jvmarg value="-Xmx128m"/>
|
||||||
|
<jvmarg value="-javaagent:${temp}/premain.jar"/>
|
||||||
|
<compilerarg value="-nowarn"/>
|
||||||
|
</kotlinc>
|
||||||
|
</target>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package hello
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
println(0 as String)
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import java.lang.instrument.Instrumentation;
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.lang.management.RuntimeMXBean;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Premain {
|
||||||
|
public static void premain(String agentArgs, Instrumentation inst) {
|
||||||
|
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
|
||||||
|
List<String> arguments = bean.getInputArguments();
|
||||||
|
arguments.stream().filter(param ->
|
||||||
|
param.startsWith("-Xmx") || param.startsWith("-Xms")
|
||||||
|
).sorted().forEach(System.out::println);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
OUT:
|
||||||
|
Buildfile: [TestData]/build.xml
|
||||||
|
|
||||||
|
premain:
|
||||||
|
[javac] Compiling 1 source file to [Temp]
|
||||||
|
[jar] Building jar: [Temp]/premain.jar
|
||||||
|
|
||||||
|
build:
|
||||||
|
[kotlinc] Compiling [[TestData]] => [[Temp]/fork.jar]
|
||||||
|
[kotlinc] -Xms64m
|
||||||
|
[kotlinc] -Xmx128m
|
||||||
|
[kotlinc] error: warnings found and -Werror specified
|
||||||
|
[kotlinc] [TestData]/test.kt:4:15: warning: this cast can never succeed
|
||||||
|
[kotlinc] println(0 as String)
|
||||||
|
[kotlinc] ^
|
||||||
|
|
||||||
|
ERR:
|
||||||
|
|
||||||
|
BUILD FAILED
|
||||||
|
[TestData]/build.xml:18: Compile failed; see the compiler error output for details.
|
||||||
|
|
||||||
|
Total time: [time]
|
||||||
|
|
||||||
|
Return code: 1
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<project name="Ant Task Test" default="build">
|
||||||
|
<taskdef resource="org/jetbrains/kotlin/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
|
||||||
|
|
||||||
|
<target name="premain">
|
||||||
|
<sequential>
|
||||||
|
<javac srcdir="${test.data}/premain" destdir="${temp}" includeAntRuntime="false"/>
|
||||||
|
<jar destfile="${temp}/premain.jar"
|
||||||
|
basedir="${temp}"
|
||||||
|
excludes="*.java *.jar">
|
||||||
|
<manifest>
|
||||||
|
<attribute name="Premain-Class" value="Premain"/>
|
||||||
|
</manifest>
|
||||||
|
</jar>
|
||||||
|
</sequential>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="build" depends="premain">
|
||||||
|
<kotlinc src="${test.data}" output="${temp}/fork.jar" fork="true">
|
||||||
|
<jvmarg value="-Xms64m"/>
|
||||||
|
<jvmarg value="-Xmx128m"/>
|
||||||
|
<jvmarg value="-javaagent:${temp}/premain.jar"/>
|
||||||
|
<compilerarg value="-Werror"/>
|
||||||
|
</kotlinc>
|
||||||
|
</target>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import java.lang.instrument.Instrumentation;
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.lang.management.RuntimeMXBean;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Premain {
|
||||||
|
public static void premain(String agentArgs, Instrumentation inst) {
|
||||||
|
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
|
||||||
|
List<String> arguments = bean.getInputArguments();
|
||||||
|
arguments.stream().filter(param ->
|
||||||
|
param.startsWith("-Xmx") || param.startsWith("-Xms")
|
||||||
|
).sorted().forEach(System.out::println);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package hello
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
println(0 as String)
|
||||||
|
}
|
||||||
+10
@@ -44,6 +44,16 @@ public class AntTaskTestGenerated extends AbstractAntTaskTest {
|
|||||||
runTest("compiler/testData/integration/ant/jvm/failOnErrorByDefault/");
|
runTest("compiler/testData/integration/ant/jvm/failOnErrorByDefault/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("fork")
|
||||||
|
public void testFork() throws Exception {
|
||||||
|
runTest("compiler/testData/integration/ant/jvm/fork/");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("forkOnError")
|
||||||
|
public void testForkOnError() throws Exception {
|
||||||
|
runTest("compiler/testData/integration/ant/jvm/forkOnError/");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("helloWorld")
|
@TestMetadata("helloWorld")
|
||||||
public void testHelloWorld() throws Exception {
|
public void testHelloWorld() throws Exception {
|
||||||
runTest("compiler/testData/integration/ant/jvm/helloWorld/");
|
runTest("compiler/testData/integration/ant/jvm/helloWorld/");
|
||||||
|
|||||||
Reference in New Issue
Block a user