Ant task: drop "jar" parameter

"output" should be used instead, analogous to "-d" in kotlinc-jvm
This commit is contained in:
Alexander Udalov
2014-07-30 16:52:58 -07:00
parent 4d25caa2e8
commit 9c792bacfa
2 changed files with 35 additions and 108 deletions
@@ -16,13 +16,13 @@
package org.jetbrains.jet.buildtools.ant; package org.jetbrains.jet.buildtools.ant;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task; import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference; import org.apache.tools.ant.types.Reference;
import org.jetbrains.jet.buildtools.core.BytecodeCompiler; import org.jetbrains.jet.buildtools.core.BytecodeCompiler;
import org.jetbrains.jet.buildtools.core.Util; import org.jetbrains.jet.buildtools.core.Util;
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentException;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@@ -41,9 +41,7 @@ import static org.jetbrains.jet.buildtools.core.Util.getPath;
* http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Javac.java?view=markup. * http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Javac.java?view=markup.
*/ */
public class BytecodeCompilerTask extends Task { public class BytecodeCompilerTask extends Task {
private File output; private File output;
private File jar;
private File stdlib; private File stdlib;
private Path src; private Path src;
private Path externalAnnotations; private Path externalAnnotations;
@@ -56,10 +54,6 @@ public class BytecodeCompilerTask extends Task {
this.output = output; this.output = output;
} }
public void setJar(File jar) {
this.jar = jar;
}
public void setStdlib(File stdlib) { public void setStdlib(File stdlib) {
this.stdlib = stdlib; this.stdlib = stdlib;
} }
@@ -141,49 +135,39 @@ public class BytecodeCompilerTask extends Task {
@Override @Override
public void execute() { public void execute() {
BytecodeCompiler compiler = new BytecodeCompiler(); BytecodeCompiler compiler = new BytecodeCompiler();
String stdlibPath = (this.stdlib != null ? getPath(this.stdlib) : null); String stdlibPath = (stdlib != null ? getPath(stdlib) : null);
String[] classpath = (this.compileClasspath != null ? this.compileClasspath.list() : null); String[] classpath = (compileClasspath != null ? compileClasspath.list() : null);
String[] externalAnnotationsPath = (this.externalAnnotations != null) ? this.externalAnnotations.list() : null; String[] externalAnnotationsPath = (externalAnnotations != null) ? externalAnnotations.list() : null;
List<String> args = new ArrayList<String>(); List<String> args = new ArrayList<String>();
for (Commandline.Argument argument : additionalArguments) { for (Commandline.Argument argument : additionalArguments) {
args.addAll(Arrays.asList(argument.getParts())); args.addAll(Arrays.asList(argument.getParts()));
} }
if (this.src != null) { if (output == null) {
throw new BuildException("\"output\" should be specified");
}
if ((this.output == null) && (this.jar == null)) { if (src != null) {
throw new CompileEnvironmentException("\"output\" or \"jar\" should be specified"); String[] source = Util.getPaths(src.list());
} String destination = getPath(output);
String[] source = Util.getPaths(this.src.list());
String destination = getPath(this.output != null ? this.output : this.jar);
log(String.format("Compiling [%s] => [%s]", Arrays.toString(source), destination)); log(String.format("Compiling [%s] => [%s]", Arrays.toString(source), destination));
compiler.compileSources(source, destination, includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args);
if (this.output != null) {
compiler.sourcesToDir(source, destination, stdlibPath, classpath, externalAnnotationsPath, args);
}
else {
compiler.sourcesToJar(source, destination, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args);
}
} }
else if (this.module != null) { else if (module != null) {
if (!output.toString().endsWith(".jar")) {
if (this.output != null) { throw new BuildException("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 modulePath = getPath(module);
String jarPath = (this.jar != null ? getPath(this.jar) : null); String jarPath = getPath(output);
log(jarPath != null ? String.format("Compiling [%s] => [%s]", modulePath, jarPath) : log(String.format("Compiling [%s] => [%s]", modulePath, jarPath));
String.format("Compiling [%s]", modulePath)); compiler.compileModule(modulePath, jarPath, includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args);
compiler.moduleToJar(modulePath, jarPath, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args);
} }
else { else {
throw new CompileEnvironmentException("\"src\" or \"module\" should be specified"); throw new BuildException("\"src\" or \"module\" should be specified");
} }
} }
} }
@@ -51,9 +51,6 @@ import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.ANNOTATIONS_PATH_KE
import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.CLASSPATH_KEY; import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.CLASSPATH_KEY;
import static org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil.loadModuleDescriptions; import static org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil.loadModuleDescriptions;
/**
* Wrapper class for Kotlin bytecode compiler.
*/
public class BytecodeCompiler { public class BytecodeCompiler {
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); private static final String LINE_SEPARATOR = System.getProperty("line.separator");
@@ -62,23 +59,6 @@ public class BytecodeCompiler {
public BytecodeCompiler() { public BytecodeCompiler() {
} }
/**
* @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
*/
private JetCoreEnvironment env(
String stdlib,
String[] classpath,
String[] externalAnnotationsPath,
String[] sourceRoots,
List<String> args
) {
return JetCoreEnvironment.createForProduction(
Disposer.newDisposable(),
createConfiguration(stdlib, classpath, externalAnnotationsPath, sourceRoots, args)
);
}
@NotNull @NotNull
private CompilerConfiguration createConfiguration( private CompilerConfiguration createConfiguration(
@Nullable String stdlib, @Nullable String stdlib,
@@ -139,13 +119,6 @@ public class BytecodeCompiler {
return configuration; return configuration;
} }
/**
* Retrieves compilation error message.
*
* @param source compilation source
* @param exceptionThrown whether compilation failed due to exception thrown
* @return compilation error message
*/
private static String errorMessage(@NotNull String[] source, boolean exceptionThrown) { private static String errorMessage(@NotNull String[] source, boolean exceptionThrown) {
return String.format("Compilation of the following source roots failed:" + LINE_SEPARATOR + return String.format("Compilation of the following source roots failed:" + LINE_SEPARATOR +
getAbsolutePaths(source) + getAbsolutePaths(source) +
@@ -165,55 +138,18 @@ public class BytecodeCompiler {
); );
} }
/** /**
* {@code CompileEnvironment#compileBunchOfSources} wrapper. * {@code KotlinToJVMBytecodeCompiler#compileBunchOfSources} wrapper.
* @param src compilation source (directories or files)
* @param output compilation destination directory
* @param stdlib "kotlin-runtime.jar" path
* @param classpath compilation classpath, can be <code>null</code> or empty
* @param args additional command line arguments to Kotlin compiler
*/
public void sourcesToDir(
@NotNull String[] src,
@NotNull String output,
@Nullable String stdlib,
@Nullable String[] classpath,
@Nullable String[] externalAnnotationsPath,
@NotNull List<String> args
) {
try {
JetCoreEnvironment environment = env(stdlib, classpath, externalAnnotationsPath, src, args);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, null, new File(output), true);
if (!success) {
throw new CompileEnvironmentException(errorMessage(src, false));
}
}
catch (BuildException e) {
throw e;
}
catch (CompileEnvironmentException e) {
throw e;
}
catch (Exception e) {
throw new CompileEnvironmentException(errorMessage(src, true), e);
}
}
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
* @param src compilation source (directory or file) * @param src compilation source (directory or file)
* @param jar compilation destination jar * @param destination compilation destination jar
* @param includeRuntime whether Kotlin runtime library is included in destination jar * @param includeRuntime whether Kotlin runtime library is included in destination jar
* @param stdlib "kotlin-runtime.jar" path * @param stdlib "kotlin-runtime.jar" path
* @param classpath compilation classpath, can be <code>null</code> or empty * @param classpath compilation classpath, can be <code>null</code> or empty
* @param args additional command line arguments to Kotlin compiler * @param args additional command line arguments to Kotlin compiler
*/ */
public void sourcesToJar( public void compileSources(
@NotNull String[] src, @NotNull String[] src,
@NotNull String jar, @NotNull String destination,
boolean includeRuntime, boolean includeRuntime,
@Nullable String stdlib, @Nullable String stdlib,
@Nullable String[] classpath, @Nullable String[] classpath,
@@ -221,9 +157,17 @@ public class BytecodeCompiler {
@NotNull List<String> args @NotNull List<String> args
) { ) {
try { try {
JetCoreEnvironment environment = env(stdlib, classpath, externalAnnotationsPath, src, args); JetCoreEnvironment environment = JetCoreEnvironment.createForProduction(
Disposer.newDisposable(),
createConfiguration(stdlib, classpath, externalAnnotationsPath, src, args)
);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, new File(jar), null, includeRuntime); // TODO: use K2JVMCompiler directly, don't duplicate this code here
boolean isJar = destination.endsWith(".jar");
File jar = isJar ? new File(destination) : null;
File outputDir = isJar ? null : new File(destination);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, includeRuntime);
if (!success) { if (!success) {
throw new CompileEnvironmentException(errorMessage(src, false)); throw new CompileEnvironmentException(errorMessage(src, false));
} }
@@ -239,9 +183,8 @@ public class BytecodeCompiler {
} }
} }
/** /**
* {@code CompileEnvironment#compileModules} wrapper. * {@code KotlinToJVMBytecodeCompiler#compileModules} wrapper.
* @param module compilation module file * @param module compilation module file
* @param jar compilation destination jar * @param jar compilation destination jar
* @param includeRuntime whether Kotlin runtime library is included in destination jar * @param includeRuntime whether Kotlin runtime library is included in destination jar
@@ -249,7 +192,7 @@ public class BytecodeCompiler {
* @param classpath compilation classpath, can be <code>null</code> or empty * @param classpath compilation classpath, can be <code>null</code> or empty
* @param args additional command line arguments to Kotlin compiler * @param args additional command line arguments to Kotlin compiler
*/ */
public void moduleToJar( public void compileModule(
@NotNull String module, @NotNull String module,
@NotNull String jar, @NotNull String jar,
boolean includeRuntime, boolean includeRuntime,