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 final BytecodeCompiler compiler = new BytecodeCompiler();
private File module;
private File srcdir; private File srcdir;
private File file; private File file;
private File destdir; private File destdir;
@@ -25,6 +26,7 @@ public class BytecodeCompilerTask extends Task {
private boolean includeRuntime = true; private boolean includeRuntime = true;
private boolean excludeStdlib = false; private boolean excludeStdlib = false;
public void setModule ( File module ) { this.module = module; }
public void setSrcdir ( File srcdir ) { this.srcdir = srcdir; } public void setSrcdir ( File srcdir ) { this.srcdir = srcdir; }
public void setFile ( File file ) { this.file = file; } public void setFile ( File file ) { this.file = file; }
public void setDestdir ( File destdir ) { this.destdir = destdir; } public void setDestdir ( File destdir ) { this.destdir = destdir; }
@@ -49,15 +51,25 @@ public class BytecodeCompilerTask extends Task {
log( String.format( "[%s] => [%s]", src, dest )); log( String.format( "[%s] => [%s]", src, dest ));
if ( this.destdir != null ) { if ( this.destdir != null ) {
compiler.sourcesToDir( src, dest, this.includeRuntime, this.excludeStdlib ); compiler.sourcesToDir( src, dest, this.excludeStdlib );
} }
else { else {
compiler.sourcesToJar( src, dest, this.includeRuntime, this.excludeStdlib ); 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 { 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"> <project name="build-tools" default="buildToolsJar">
<property name="tests-dir" location="${output}/ant-test/build-tools-test"/> <property name="tests-dir" location="${output}/ant-test/build-tools-test"/>
<property name="kotlin-home" location="${output}/ant-test/kotlin-home"/> <property name="kotlin-home" location="${output}/ant-test/kotlin-home"/>
<property name="tests-jar" location="${tests-dir}/out.jar"/> <property name="tests-jar" location="${tests-dir}/out.jar"/>
<property name="bootstrap-dir" location="${basedir}/bootstrap.compiler"/> <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"> <taskdef resource="net/sf/antcontrib/antlib.xml">
@@ -14,8 +14,8 @@
</taskdef> </taskdef>
<!-- Re-creates ${kotlin-home} and defines <kotlin> tasks using "${kotlin-home}/lib" jars --> <!-- Creates ${kotlin-home} and defines <kotlin> task -->
<macrodef name="setup-kotlin-home"> <macrodef name="define-kotlinc">
<sequential> <sequential>
<if> <if>
@@ -65,14 +65,23 @@
</macrodef> </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"> <macrodef name="kotlinc-dir">
<attribute name="file" default=""/> <attribute name="file" default=""/>
<attribute name="srcdir" default=""/> <attribute name="srcdir" default=""/>
<sequential> <sequential>
<delete dir = "${tests-dir}"/> <cleanup/>
<mkdir dir = "${tests-dir}"/>
<if> <if>
<equals arg1="@{file}" arg2=""/> <equals arg1="@{file}" arg2=""/>
@@ -87,23 +96,40 @@
</macrodef> </macrodef>
<!-- Runs <kotlinc> with test jar --> <!-- Runs <kotlinc> to create a *.class files in a jar -->
<macrodef name="kotlinc-jar"> <macrodef name="kotlinc-jar">
<attribute name="file" default=""/> <attribute name="file" default=""/>
<attribute name="srcdir" default=""/> <attribute name="srcdir" default=""/>
<attribute name="module" default=""/>
<sequential> <sequential>
<delete dir = "${tests-dir}"/> <cleanup/>
<mkdir dir = "${tests-dir}"/>
<delete file = "${tests-jar}"/>
<if> <if>
<equals arg1="@{file}" arg2=""/> <not>
<equals arg1="@{file}" arg2=""/>
</not>
<then> <then>
<kotlinc srcdir = "@{srcdir}" destjar = "${tests-jar}"/> <kotlinc file = "@{file}" destjar = "${tests-jar}"/>
</then> </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> <else>
<kotlinc file = "@{file}" destjar = "${tests-jar}"/> <fail message="One of 'file', 'srcdir', or 'module' should be specified"/>
</else> </else>
</if> </if>
</sequential> </sequential>
@@ -123,10 +149,10 @@
<if> <if>
<istrue value="@{run-jar}"/> <istrue value="@{run-jar}"/>
<then> <then>
<echo>Running [@{classname}], cp = [${tests-jar}]</echo> <echo>Running [@{classname}], classpath = "${tests-jar}"</echo>
<java outputproperty = "java-out" <java outputproperty = "java-out"
classname = "@{classname}" classname = "@{classname}"
failonerror = "true"> failonerror = "${failonerror}">
<classpath> <classpath>
<pathelement path = "${tests-jar}"/> <pathelement path = "${tests-jar}"/>
</classpath> </classpath>
@@ -134,10 +160,10 @@
</java> </java>
</then> </then>
<else> <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" <java outputproperty = "java-out"
classname = "@{classname}" classname = "@{classname}"
failonerror = "true"> failonerror = "${failonerror}">
<classpath> <classpath>
<pathelement path = "${tests-dir}"/> <pathelement path = "${tests-dir}"/>
<fileset file = "${kotlin-home}/lib/kotlin-runtime.jar"/> <fileset file = "${kotlin-home}/lib/kotlin-runtime.jar"/>
@@ -158,9 +184,12 @@
<then> <then>
<echo>${java-out}</echo> <echo>${java-out}</echo>
</then> </then>
<else> <elseif>
<fail message="Test failed: '${java-out}' (received) != '@{out}' (expected), equals = [@{equals}]"/> <istrue value="${failonerror}"/>
</else> <then>
<fail message="Test failed: '${java-out}' (received) != '@{out}' (expected), equals = [@{equals}]"/>
</then>
</elseif>
</if> </if>
</sequential> </sequential>
</macrodef> </macrodef>
@@ -205,17 +234,26 @@
<sequential> <sequential>
<kotlinc-dir srcdir = "${basedir}/build-tools/test/longer-examples"/> <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 = "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 = "maze.namespace" equals="false" out="O ~OOOOOOOOOOOOOO"/>
<!--<run-java classname = "life.namespace" equals="false" out="*** ** ** ***"/>--> <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 = "html.namespace" equals="false" out="&lt;a href=&quot;http://jetbrains.com/kotlin&quot;&gt;"/>
<kotlinc-jar srcdir = "${basedir}/build-tools/test/longer-examples"/> <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 = "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 = "maze.namespace" equals="false" run-jar="true" out="O ~OOOOOOOOOOOOOO"/>
<!--<run-java classname = "life.namespace" equals="false" run-jar="true" out="*** ** ** ***"/>--> <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 = "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> </sequential>
</macrodef> </macrodef>
@@ -287,7 +325,7 @@
</target> </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"> <target name="buildToolsBootstrap">
<delete dir="${bootstrap-dir}" failonerror="true"/> <delete dir="${bootstrap-dir}" failonerror="true"/>
<mkdir dir="${bootstrap-dir}"/> <mkdir dir="${bootstrap-dir}"/>
@@ -298,9 +336,10 @@
<!-- Tests build tools distribution jar --> <!-- Tests build tools distribution jar -->
<target name="buildToolsTest"> <target name="buildToolsTest">
<setup-kotlin-home/> <define-kotlinc/>
<hello-tests/> <hello-tests/>
<longer-examples-tests/> <longer-examples-tests/>
<module-tests/>
<compilation-fail-test/> <compilation-fail-test/>
</target> </target>
</project> </project>
@@ -45,11 +45,12 @@ public class BytecodeCompiler {
/** /**
* {@code CompileEnvironment#compileBunchOfSources} wrapper. * {@code CompileEnvironment#compileBunchOfSources} wrapper.
* *
* @param source compilation source (directory or file) * @param source compilation source (directory or file)
* @param destination compilation destination * @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 ) { public void sourcesToDir ( String source, String destination, boolean excludeStdlib ) {
boolean success = ENV.compileBunchOfSources( source, null, destination, includeRuntime, ! excludeStdlib ); boolean success = ENV.compileBunchOfSources( source, null, destination, true, ! excludeStdlib );
if ( ! success ) { if ( ! success ) {
throw new RuntimeException( compilationError( source )); throw new RuntimeException( compilationError( source ));
} }
@@ -59,8 +60,10 @@ public class BytecodeCompiler {
/** /**
* {@code CompileEnvironment#compileBunchOfSources} wrapper. * {@code CompileEnvironment#compileBunchOfSources} wrapper.
* *
* @param source compilation source (directory or file) * @param source compilation source (directory or file)
* @param jar compilation destination jar * @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 ) { public void sourcesToJar ( String source, String jar, boolean includeRuntime, boolean excludeStdlib ) {
boolean success = ENV.compileBunchOfSources( source, jar, null, includeRuntime, ! excludeStdlib ); boolean success = ENV.compileBunchOfSources( source, jar, null, includeRuntime, ! excludeStdlib );
@@ -68,4 +71,16 @@ public class BytecodeCompiler {
throw new RuntimeException( compilationError( source )); 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"
}
}