diff --git a/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/BytecodeCompilerTask.java b/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/BytecodeCompilerTask.java index 53be409abd5..35b8374fb31 100644 --- a/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/BytecodeCompilerTask.java +++ b/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/BytecodeCompilerTask.java @@ -16,13 +16,13 @@ package org.jetbrains.jet.buildtools.ant; +import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Commandline; 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.buildtools.core.Util; -import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentException; import java.io.File; 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. */ public class BytecodeCompilerTask extends Task { - private File output; - private File jar; private File stdlib; private Path src; private Path externalAnnotations; @@ -56,10 +54,6 @@ public class BytecodeCompilerTask extends Task { this.output = output; } - public void setJar(File jar) { - this.jar = jar; - } - public void setStdlib(File stdlib) { this.stdlib = stdlib; } @@ -141,49 +135,39 @@ public class BytecodeCompilerTask extends Task { @Override public void execute() { BytecodeCompiler compiler = new BytecodeCompiler(); - String stdlibPath = (this.stdlib != null ? getPath(this.stdlib) : null); - String[] classpath = (this.compileClasspath != null ? this.compileClasspath.list() : null); - String[] externalAnnotationsPath = (this.externalAnnotations != null) ? this.externalAnnotations.list() : null; + String stdlibPath = (stdlib != null ? getPath(stdlib) : null); + String[] classpath = (compileClasspath != null ? compileClasspath.list() : null); + String[] externalAnnotationsPath = (externalAnnotations != null) ? externalAnnotations.list() : null; List args = new ArrayList(); for (Commandline.Argument argument : additionalArguments) { 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)) { - throw new CompileEnvironmentException("\"output\" or \"jar\" should be specified"); - } - - String[] source = Util.getPaths(this.src.list()); - String destination = getPath(this.output != null ? this.output : this.jar); + if (src != null) { + String[] source = Util.getPaths(src.list()); + String destination = getPath(output); log(String.format("Compiling [%s] => [%s]", Arrays.toString(source), destination)); - - if (this.output != null) { - compiler.sourcesToDir(source, destination, stdlibPath, classpath, externalAnnotationsPath, args); - } - else { - compiler.sourcesToJar(source, destination, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args); - } + compiler.compileSources(source, destination, includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args); } - else if (this.module != null) { - - if (this.output != null) { - throw new CompileEnvironmentException("Module compilation is only supported for jar destination"); + else if (module != null) { + if (!output.toString().endsWith(".jar")) { + throw new BuildException("Module compilation is only supported for jar destination"); } - String modulePath = getPath(this.module); - String jarPath = (this.jar != null ? getPath(this.jar) : null); + String modulePath = getPath(module); + String jarPath = getPath(output); - log(jarPath != null ? String.format("Compiling [%s] => [%s]", modulePath, jarPath) : - String.format("Compiling [%s]", modulePath)); - - compiler.moduleToJar(modulePath, jarPath, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args); + log(String.format("Compiling [%s] => [%s]", modulePath, jarPath)); + compiler.compileModule(modulePath, jarPath, includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args); } else { - throw new CompileEnvironmentException("\"src\" or \"module\" should be specified"); + throw new BuildException("\"src\" or \"module\" should be specified"); } } } diff --git a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java index a42154446f4..f7aeed973bb 100644 --- a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java +++ b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java @@ -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.compiler.CompileEnvironmentUtil.loadModuleDescriptions; -/** - * Wrapper class for Kotlin bytecode compiler. - */ public class BytecodeCompiler { private static final String LINE_SEPARATOR = System.getProperty("line.separator"); @@ -62,23 +59,6 @@ public class 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 args - ) { - return JetCoreEnvironment.createForProduction( - Disposer.newDisposable(), - createConfiguration(stdlib, classpath, externalAnnotationsPath, sourceRoots, args) - ); - } - @NotNull private CompilerConfiguration createConfiguration( @Nullable String stdlib, @@ -139,13 +119,6 @@ public class BytecodeCompiler { 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) { return String.format("Compilation of the following source roots failed:" + LINE_SEPARATOR + getAbsolutePaths(source) + @@ -165,55 +138,18 @@ public class BytecodeCompiler { ); } - /** - * {@code CompileEnvironment#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 null 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 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. + * {@code KotlinToJVMBytecodeCompiler#compileBunchOfSources} wrapper. * @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 stdlib "kotlin-runtime.jar" path * @param classpath compilation classpath, can be null or empty * @param args additional command line arguments to Kotlin compiler */ - public void sourcesToJar( + public void compileSources( @NotNull String[] src, - @NotNull String jar, + @NotNull String destination, boolean includeRuntime, @Nullable String stdlib, @Nullable String[] classpath, @@ -221,9 +157,17 @@ public class BytecodeCompiler { @NotNull List args ) { 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) { 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 jar compilation 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 null or empty * @param args additional command line arguments to Kotlin compiler */ - public void moduleToJar( + public void compileModule( @NotNull String module, @NotNull String jar, boolean includeRuntime,