Ant task - <kotlinc module=".."/> support + test

This commit is contained in:
Evgeny Goldin
2012-01-22 20:48:55 +02:00
parent 7356abf447
commit 1030fcf016
5 changed files with 122 additions and 44 deletions
@@ -18,6 +18,7 @@ public class BytecodeCompilerTask extends Task {
private final BytecodeCompiler compiler = new BytecodeCompiler();
private File module;
private File srcdir;
private File file;
private File destdir;
@@ -25,6 +26,7 @@ public class BytecodeCompilerTask extends Task {
private boolean includeRuntime = true;
private boolean excludeStdlib = false;
public void setModule ( File module ) { this.module = module; }
public void setSrcdir ( File srcdir ) { this.srcdir = srcdir; }
public void setFile ( File file ) { this.file = file; }
public void setDestdir ( File destdir ) { this.destdir = destdir; }
@@ -49,15 +51,25 @@ public class BytecodeCompilerTask extends Task {
log( String.format( "[%s] => [%s]", src, dest ));
if ( this.destdir != null ) {
compiler.sourcesToDir( src, dest, this.includeRuntime, this.excludeStdlib );
compiler.sourcesToDir( src, dest, this.excludeStdlib );
}
else {
compiler.sourcesToJar( src, dest, this.includeRuntime, this.excludeStdlib );
}
}
else if ( this.module != null ) {
if ( this.destdir != null ) {
throw new RuntimeException( "Module compilation is only supported for jar destination" );
}
String modulePath = getPath( this.module );
String jarPath = ( this.destjar != null ? getPath( this.destjar ) : null );
compiler.moduleToJar( modulePath, jarPath, this.includeRuntime );
}
else {
throw new RuntimeException( "Operation is not supported yet" );
throw new RuntimeException( "This operation is not supported" );
}
}
}
+75 -36
View File
@@ -1,10 +1,10 @@
<project name="build-tools" default="buildToolsJar">
<property name="tests-dir" location="${output}/ant-test/build-tools-test"/>
<property name="kotlin-home" location="${output}/ant-test/kotlin-home"/>
<property name="tests-jar" location="${tests-dir}/out.jar"/>
<property name="bootstrap-dir" location="${basedir}/bootstrap.compiler"/>
<property name="tests-dir" location="${output}/ant-test/build-tools-test"/>
<property name="kotlin-home" location="${output}/ant-test/kotlin-home"/>
<property name="tests-jar" location="${tests-dir}/out.jar"/>
<property name="bootstrap-dir" location="${basedir}/bootstrap.compiler"/>
<property name="failonerror" value="false"/> <!-- Whether invoking compiled classes should file on error -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
@@ -14,8 +14,8 @@
</taskdef>
<!-- Re-creates ${kotlin-home} and defines <kotlin> tasks using "${kotlin-home}/lib" jars -->
<macrodef name="setup-kotlin-home">
<!-- Creates ${kotlin-home} and defines <kotlin> task -->
<macrodef name="define-kotlinc">
<sequential>
<if>
@@ -65,14 +65,23 @@
</macrodef>
<!-- Runs <kotlinc> in test directory -->
<!-- Deletes all previously compiled files -->
<macrodef name="cleanup">
<sequential>
<delete dir = "${tests-dir}" failonerror="true"/>
<delete file = "${tests-jar}" failonerror="true"/>
<mkdir dir = "${tests-dir}"/>
</sequential>
</macrodef>
<!-- Runs <kotlinc> create a *.class files in directory -->
<macrodef name="kotlinc-dir">
<attribute name="file" default=""/>
<attribute name="srcdir" default=""/>
<sequential>
<delete dir = "${tests-dir}"/>
<mkdir dir = "${tests-dir}"/>
<cleanup/>
<if>
<equals arg1="@{file}" arg2=""/>
@@ -87,23 +96,40 @@
</macrodef>
<!-- Runs <kotlinc> with test jar -->
<!-- Runs <kotlinc> to create a *.class files in a jar -->
<macrodef name="kotlinc-jar">
<attribute name="file" default=""/>
<attribute name="srcdir" default=""/>
<attribute name="file" default=""/>
<attribute name="srcdir" default=""/>
<attribute name="module" default=""/>
<sequential>
<delete dir = "${tests-dir}"/>
<mkdir dir = "${tests-dir}"/>
<delete file = "${tests-jar}"/>
<cleanup/>
<if>
<equals arg1="@{file}" arg2=""/>
<not>
<equals arg1="@{file}" arg2=""/>
</not>
<then>
<kotlinc srcdir = "@{srcdir}" destjar = "${tests-jar}"/>
<kotlinc file = "@{file}" destjar = "${tests-jar}"/>
</then>
<elseif>
<not>
<equals arg1="@{srcdir}" arg2=""/>
</not>
<then>
<kotlinc srcdir = "@{srcdir}" destjar = "${tests-jar}"/>
</then>
</elseif>
<elseif>
<not>
<equals arg1="@{module}" arg2=""/>
</not>
<then>
<kotlinc module = "@{module}" destjar = "${tests-jar}"/>
</then>
</elseif>
<else>
<kotlinc file = "@{file}" destjar = "${tests-jar}"/>
<fail message="One of 'file', 'srcdir', or 'module' should be specified"/>
</else>
</if>
</sequential>
@@ -123,10 +149,10 @@
<if>
<istrue value="@{run-jar}"/>
<then>
<echo>Running [@{classname}], cp = [${tests-jar}]</echo>
<echo>Running [@{classname}], classpath = "${tests-jar}"</echo>
<java outputproperty = "java-out"
classname = "@{classname}"
failonerror = "true">
failonerror = "${failonerror}">
<classpath>
<pathelement path = "${tests-jar}"/>
</classpath>
@@ -134,10 +160,10 @@
</java>
</then>
<else>
<echo>Running [@{classname}], cp = [${tests-dir}];[${kotlin-home}/lib/kotlin-runtime.jar]</echo>
<echo>Running [@{classname}], classpath = "${tests-dir};${kotlin-home}/lib/kotlin-runtime.jar"</echo>
<java outputproperty = "java-out"
classname = "@{classname}"
failonerror = "true">
failonerror = "${failonerror}">
<classpath>
<pathelement path = "${tests-dir}"/>
<fileset file = "${kotlin-home}/lib/kotlin-runtime.jar"/>
@@ -158,9 +184,12 @@
<then>
<echo>${java-out}</echo>
</then>
<else>
<fail message="Test failed: '${java-out}' (received) != '@{out}' (expected), equals = [@{equals}]"/>
</else>
<elseif>
<istrue value="${failonerror}"/>
<then>
<fail message="Test failed: '${java-out}' (received) != '@{out}' (expected), equals = [@{equals}]"/>
</then>
</elseif>
</if>
</sequential>
</macrodef>
@@ -205,17 +234,26 @@
<sequential>
<kotlinc-dir srcdir = "${basedir}/build-tools/test/longer-examples"/>
<!--<run-java classname = "bottles.namespace" equals="false" out="12 bottles of beer on the wall, 12 bottles of beer."/>-->
<!--<run-java classname = "maze.namespace" equals="false" out="O ~OOOOOOOOOOOOOO"/>-->
<!--<run-java classname = "life.namespace" equals="false" out="*** ** ** ***"/>-->
<!--<run-java classname = "html.namespace" equals="false" out="&lt;a href=&quot;http://jetbrains.com/kotlin&quot;&gt;"/>-->
<run-java classname = "bottles.namespace" equals="false" out="12 bottles of beer on the wall, 12 bottles of beer."/>
<run-java classname = "maze.namespace" equals="false" out="O ~OOOOOOOOOOOOOO"/>
<run-java classname = "life.namespace" equals="false" out="*** ** ** ***"/>
<run-java classname = "html.namespace" equals="false" out="&lt;a href=&quot;http://jetbrains.com/kotlin&quot;&gt;"/>
<kotlinc-jar srcdir = "${basedir}/build-tools/test/longer-examples"/>
<!--<run-java classname = "bottles.namespace" equals="false" run-jar="true" out="12 bottles of beer on the wall, 12 bottles of beer."/>-->
<!--<run-java classname = "maze.namespace" equals="false" run-jar="true" out="O ~OOOOOOOOOOOOOO"/>-->
<!--<run-java classname = "life.namespace" equals="false" run-jar="true" out="*** ** ** ***"/>-->
<!--<run-java classname = "html.namespace" equals="false" run-jar="true" out="&lt;a href=&quot;http://jetbrains.com/kotlin&quot;&gt;"/>-->
<run-java classname = "bottles.namespace" equals="false" run-jar="true" out="12 bottles of beer on the wall, 12 bottles of beer."/>
<run-java classname = "maze.namespace" equals="false" run-jar="true" out="O ~OOOOOOOOOOOOOO"/>
<run-java classname = "life.namespace" equals="false" run-jar="true" out="*** ** ** ***"/>
<run-java classname = "html.namespace" equals="false" run-jar="true" out="&lt;a href=&quot;http://jetbrains.com/kotlin&quot;&gt;"/>
</sequential>
</macrodef>
<!-- Compiles and runs a Kotlin module -->
<macrodef name="module-tests">
<sequential>
<kotlinc-jar module="${basedir}/build-tools/test/modules/smoke/Smoke.kts"/>
<run-java classname = "Smoke.namespace" run-jar = "true" args = "1 2 3" out = "1|2|3"/>
</sequential>
</macrodef>
@@ -287,7 +325,7 @@
</target>
<!-- Simulates a bootstrap environment as in TeamCity build -->
<!-- Invoked manually in a dev environment - simulates a bootstrap environment as in TeamCity build -->
<target name="buildToolsBootstrap">
<delete dir="${bootstrap-dir}" failonerror="true"/>
<mkdir dir="${bootstrap-dir}"/>
@@ -298,9 +336,10 @@
<!-- Tests build tools distribution jar -->
<target name="buildToolsTest">
<setup-kotlin-home/>
<define-kotlinc/>
<hello-tests/>
<longer-examples-tests/>
<module-tests/>
<compilation-fail-test/>
</target>
</project>
@@ -45,11 +45,12 @@ public class BytecodeCompiler {
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
*
* @param source compilation source (directory or file)
* @param destination compilation destination
* @param source compilation source (directory or file)
* @param destination compilation destination
* @param excludeStdlib whether Kotlin standard library is excluded in compilation
*/
public void sourcesToDir ( String source, String destination, boolean includeRuntime, boolean excludeStdlib ) {
boolean success = ENV.compileBunchOfSources( source, null, destination, includeRuntime, ! excludeStdlib );
public void sourcesToDir ( String source, String destination, boolean excludeStdlib ) {
boolean success = ENV.compileBunchOfSources( source, null, destination, true, ! excludeStdlib );
if ( ! success ) {
throw new RuntimeException( compilationError( source ));
}
@@ -59,8 +60,10 @@ public class BytecodeCompiler {
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
*
* @param source compilation source (directory or file)
* @param jar compilation destination jar
* @param source compilation source (directory or file)
* @param jar compilation destination jar
* @param includeRuntime whether Kotlin runtime library is included in destination jar
* @param excludeStdlib whether Kotlin standard library is excluded in compilation
*/
public void sourcesToJar ( String source, String jar, boolean includeRuntime, boolean excludeStdlib ) {
boolean success = ENV.compileBunchOfSources( source, jar, null, includeRuntime, ! excludeStdlib );
@@ -68,4 +71,16 @@ public class BytecodeCompiler {
throw new RuntimeException( compilationError( source ));
}
}
/**
* {@code CompileEnvironment#compileModuleScript} wrapper.
*
* @param moduleFile compilation module file
* @param jar compilation destination jar
* @param includeRuntime whether Kotlin runtime library is included in destination jar
*/
public void moduleToJar ( String moduleFile, String jar, boolean includeRuntime ) {
ENV.compileModuleScript( moduleFile, jar, includeRuntime );
}
}
+5
View File
@@ -0,0 +1,5 @@
package Smoke
fun main(args: Array<String>) {
print("${args[0]}|${args[1]}|${args[2]}")
}
+7
View File
@@ -0,0 +1,7 @@
import kotlin.modules.*
fun project() {
module("smoke") {
sources += "Smoke.kt"
}
}