Ant task - added classpath/classpathref

CompileEnvironment - added addToClasspath(File[] / String[])
Switching to CompileEnvironmentException to throw fatal errors
This commit is contained in:
Evgeny Goldin
2012-01-28 00:13:06 +02:00
parent 8a79088698
commit 5cc9ce8a98
3 changed files with 107 additions and 33 deletions
@@ -2,7 +2,10 @@ package org.jetbrains.jet.buildtools.ant;
import static org.jetbrains.jet.buildtools.core.Util.*;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.jetbrains.jet.buildtools.core.BytecodeCompiler;
import org.jetbrains.jet.compiler.CompileEnvironmentException;
import java.io.File;
@@ -12,7 +15,8 @@ import java.io.File;
*
* See
* http://evgeny-goldin.org/javadoc/ant/tutorial-writing-tasks.html
* http://evgeny-goldin.org/javadoc/ant/Tasks/javac.html - attribute names should be similar to {@code <javac>}.
* http://evgeny-goldin.org/javadoc/ant/develop.html
* http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Javac.java?view=markup.
*/
public class BytecodeCompilerTask extends Task {
@@ -21,6 +25,7 @@ public class BytecodeCompilerTask extends Task {
private File stdlib;
private File src;
private File module;
private Path compileClasspath;
private boolean includeRuntime = true;
public void setOutput ( File output ) { this.output = output; }
@@ -31,15 +36,44 @@ public class BytecodeCompilerTask extends Task {
public void setIncludeRuntime ( boolean includeRuntime ) { this.includeRuntime = includeRuntime; }
/**
* Set the classpath to be used for this compilation.
*
* @param classpath an Ant Path object containing the compilation classpath.
*/
public void setClasspath ( Path classpath ) {
if ( this.compileClasspath == null ) {
this.compileClasspath = classpath;
}
else {
this.compileClasspath.append( classpath );
}
}
/**
* Adds a reference to a classpath defined elsewhere.
* @param ref a reference to a classpath.
*/
public void setClasspathRef( Reference ref ) {
if ( this.compileClasspath == null ) {
this.compileClasspath = new Path( getProject());
}
this.compileClasspath.createPath().setRefid( ref );
}
@Override
public void execute() {
final BytecodeCompiler compiler = new BytecodeCompiler( this.stdlib != null ? getPath( this.stdlib ) : null );
final BytecodeCompiler compiler = new BytecodeCompiler();
final String stdlibPath = ( this.stdlib != null ? getPath( this.stdlib ) : null );
final String[] classpath = ( this.compileClasspath != null ? this.compileClasspath.list() : null );
if ( this.src != null ) {
if (( this.output == null ) && ( this.jar == null )) {
throw new RuntimeException( "\"output\" or \"jar\" should be specified" );
throw new CompileEnvironmentException( "\"output\" or \"jar\" should be specified" );
}
String source = getPath( this.src );
@@ -48,25 +82,25 @@ public class BytecodeCompilerTask extends Task {
log( String.format( "[%s] => [%s]", source, destination ));
if ( this.output != null ) {
compiler.sourcesToDir( source, destination );
compiler.sourcesToDir( source, destination, stdlibPath, classpath );
}
else {
compiler.sourcesToJar( source, destination, this.includeRuntime );
compiler.sourcesToJar( source, destination, this.includeRuntime, stdlibPath, classpath );
}
}
else if ( this.module != null ) {
if ( this.output != null ) {
throw new RuntimeException( "Module compilation is only supported for jar destination" );
throw new CompileEnvironmentException( "Module compilation is only supported for jar destination" );
}
String modulePath = getPath( this.module );
String jarPath = ( this.jar != null ? getPath( this.jar ) : null );
compiler.moduleToJar( modulePath, jarPath, this.includeRuntime );
compiler.moduleToJar( modulePath, jarPath, this.includeRuntime, stdlibPath, classpath );
}
else {
throw new RuntimeException( "\"src\" or \"module\" should be specified" );
throw new CompileEnvironmentException( "\"src\" or \"module\" should be specified" );
}
}
}
@@ -1,6 +1,7 @@
package org.jetbrains.jet.buildtools.core;
import org.jetbrains.jet.compiler.CompileEnvironment;
import org.jetbrains.jet.compiler.CompileEnvironmentException;
/**
@@ -8,18 +9,31 @@ import org.jetbrains.jet.compiler.CompileEnvironment;
*/
public class BytecodeCompiler {
private final CompileEnvironment ENV;
public BytecodeCompiler () {
}
/**
* Initializes an instance of this bytecode compiler.
* @param stdlib "stdlib" compiler argument, path to "kotlin-runtime.jar", allowed to be <code>null</code> or empty.
* Creates new instance of {@link CompileEnvironment} instance using the arguments specified.
*
* @param stdlib path to "kotlin-runtime.jar", only used if not null and not empty
* @param classpath compilation classpath, only used if not null and not empty
*
* @return compile environment instance
*/
public BytecodeCompiler ( String stdlib ) {
ENV = new CompileEnvironment();
private CompileEnvironment env( String stdlib, String[] classpath ) {
CompileEnvironment env = new CompileEnvironment();
if (( stdlib != null ) && ( stdlib.trim().length() > 0 )) {
ENV.setStdlib( stdlib );
env.setStdlib( stdlib );
}
if (( classpath != null ) && ( classpath.length > 0 )) {
env.addToClasspath( classpath );
}
return env;
}
@@ -36,13 +50,15 @@ public class BytecodeCompiler {
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
*
* @param source compilation source (directory or file)
* @param destination compilation destination
* @param src compilation source (directory or file)
* @param output compilation destination directory
* @param stdlib "kotlin-runtime.jar" path
* @param classpath compilation classpath, can be <code>null</code> or empty
*/
public void sourcesToDir ( String source, String destination ) {
boolean success = ENV.compileBunchOfSources( source, null, destination, true );
public void sourcesToDir ( String src, String output, String stdlib, String[] classpath ) {
boolean success = env( stdlib, classpath ).compileBunchOfSources( src, null, output, true /* Last arg is ignored anyway */ );
if ( ! success ) {
throw new RuntimeException( compilationError( source ));
throw new CompileEnvironmentException( compilationError( src ));
}
}
@@ -50,14 +66,16 @@ public class BytecodeCompiler {
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
*
* @param source compilation source (directory or file)
* @param src compilation source (directory or file)
* @param jar compilation destination jar
* @param includeRuntime whether Kotlin runtime library is included in destination jar
* @param stdlib "kotlin-runtime.jar" path
* @param classpath compilation classpath, can be <code>null</code> or empty
*/
public void sourcesToJar ( String source, String jar, boolean includeRuntime ) {
boolean success = ENV.compileBunchOfSources( source, jar, null, includeRuntime );
public void sourcesToJar ( String src, String jar, boolean includeRuntime, String stdlib, String[] classpath ) {
boolean success = env( stdlib, classpath ).compileBunchOfSources( src, jar, null, includeRuntime );
if ( ! success ) {
throw new RuntimeException( compilationError( source ));
throw new CompileEnvironmentException( compilationError( src ));
}
}
@@ -65,11 +83,13 @@ public class BytecodeCompiler {
/**
* {@code CompileEnvironment#compileModuleScript} wrapper.
*
* @param moduleFile compilation module file
* @param module compilation module file
* @param jar compilation destination jar
* @param includeRuntime whether Kotlin runtime library is included in destination jar
* @param stdlib "kotlin-runtime.jar" path
* @param classpath compilation classpath, can be <code>null</code> or empty
*/
public void moduleToJar ( String moduleFile, String jar, boolean includeRuntime ) {
ENV.compileModuleScript( moduleFile, jar, includeRuntime );
public void moduleToJar ( String module, String jar, boolean includeRuntime, String stdlib, String[] classpath ) {
env( stdlib, classpath ).compileModuleScript( module, jar, includeRuntime );
}
}
@@ -335,19 +335,39 @@ public class CompileEnvironment {
}
}
public void setStdlib(String stdlib) {
File path = new File(stdlib);
if (!path.exists()) {
throw new CompileEnvironmentException("'" + stdlib + "' does not exist");
/**
* Add path specified to the compilation environment.
* @param paths paths to add
*/
public void addToClasspath(File ... paths) {
for (File path : paths) {
if ( ! path.exists()) {
throw new CompileEnvironmentException("'" + path + "' does not exist");
}
myEnvironment.addToClasspath(path);
}
}
/**
* Add path specified to the compilation environment.
* @param paths paths to add
*/
public void addToClasspath(String ... paths) {
for (String path : paths) {
addToClasspath( new File(path));
}
}
public void setStdlib(String stdlib) {
File file = new File(stdlib);
addToClasspath(file);
try {
myStdlib = path.toURL();
myStdlib = file.toURL();
}
catch (MalformedURLException e) {
throw new RuntimeException(e);
}
myEnvironment.addToClasspath(path);
}
}