Pull some code from K2JVMCompiler to CLICompiler. Also requires a hack due to compiler bug.
This commit is contained in:
@@ -18,7 +18,10 @@ package org.jetbrains.jet.cli.common;
|
|||||||
|
|
||||||
import com.sampullara.cli.Args;
|
import com.sampullara.cli.Args;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation;
|
||||||
|
import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity;
|
||||||
import org.jetbrains.jet.cli.common.messages.MessageRenderer;
|
import org.jetbrains.jet.cli.common.messages.MessageRenderer;
|
||||||
|
import org.jetbrains.jet.cli.jvm.K2JVMCompilerVersion;
|
||||||
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentException;
|
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentException;
|
||||||
|
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
@@ -78,8 +81,44 @@ public abstract class CLICompiler<CLArgs extends CompilerArguments, CEConf exten
|
|||||||
@NotNull
|
@NotNull
|
||||||
protected abstract CLArgs createArguments();
|
protected abstract CLArgs createArguments();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the compiler on the parsed arguments
|
||||||
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public abstract ExitCode exec(PrintStream errStream, CLArgs arguments);
|
public ExitCode exec(final PrintStream errStream, CLArgs arguments) {
|
||||||
|
if (arguments.isHelp()) {
|
||||||
|
usage(errStream);
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
System.setProperty("java.awt.headless", "true");
|
||||||
|
final MessageRenderer messageRenderer = getMessageRenderer(arguments);
|
||||||
|
errStream.print(messageRenderer.renderPreamble());
|
||||||
|
try {
|
||||||
|
return doExecute(errStream, arguments, messageRenderer);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
errStream.print(messageRenderer.renderConclusion());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: can't declare parameters as not null due to KT-1863
|
||||||
|
@NotNull
|
||||||
|
protected abstract ExitCode doExecute(PrintStream stream, CLArgs arguments, MessageRenderer renderer);
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private MessageRenderer getMessageRenderer(@NotNull CLArgs arguments) {
|
||||||
|
return arguments.isTags() ? MessageRenderer.TAGS : MessageRenderer.PLAIN;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void printVersionIfNeeded(@NotNull PrintStream errStream,
|
||||||
|
@NotNull CLArgs arguments,
|
||||||
|
@NotNull MessageRenderer messageRenderer) {
|
||||||
|
if (arguments.isVersion()) {
|
||||||
|
errStream.println(messageRenderer
|
||||||
|
.render(CompilerMessageSeverity.INFO, "Kotlin Compiler version " + K2JVMCompilerVersion.VERSION,
|
||||||
|
CompilerMessageLocation.NO_LOCATION));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Useful main for derived command line tools
|
* Useful main for derived command line tools
|
||||||
|
|||||||
@@ -51,112 +51,92 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, CompileEn
|
|||||||
doMain(new K2JVMCompiler(), args);
|
doMain(new K2JVMCompiler(), args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes the compiler on the parsed arguments
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
@Override
|
@Override
|
||||||
public ExitCode exec(final PrintStream errStream, K2JVMCompilerArguments arguments) {
|
@NotNull
|
||||||
if (arguments.help) {
|
protected ExitCode doExecute(PrintStream errStream,
|
||||||
usage(errStream);
|
K2JVMCompilerArguments arguments,
|
||||||
return OK;
|
MessageRenderer messageRenderer) {
|
||||||
|
printVersionIfNeeded(errStream, arguments, messageRenderer);
|
||||||
|
|
||||||
|
CompilerSpecialMode mode = parseCompilerSpecialMode(arguments);
|
||||||
|
|
||||||
|
File jdkHeadersJar;
|
||||||
|
if (mode.includeJdkHeaders()) {
|
||||||
|
if (arguments.jdkHeaders != null) {
|
||||||
|
jdkHeadersJar = new File(arguments.jdkHeaders);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
jdkHeadersJar = PathUtil.getAltHeadersPath();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
System.setProperty("java.awt.headless", "true");
|
else {
|
||||||
|
jdkHeadersJar = null;
|
||||||
|
}
|
||||||
|
File runtimeJar;
|
||||||
|
|
||||||
final MessageRenderer messageRenderer = arguments.tags ? MessageRenderer.TAGS : MessageRenderer.PLAIN;
|
if (mode.includeKotlinRuntime()) {
|
||||||
|
if (arguments.stdlib != null) {
|
||||||
|
runtimeJar = new File(arguments.stdlib);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
runtimeJar = PathUtil.getDefaultRuntimePath();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
runtimeJar = null;
|
||||||
|
}
|
||||||
|
|
||||||
errStream.print(messageRenderer.renderPreamble());
|
CompilerDependencies dependencies = new CompilerDependencies(mode, CompilerDependencies.findRtJar(), jdkHeadersJar, runtimeJar);
|
||||||
|
PrintingMessageCollector messageCollector = new PrintingMessageCollector(errStream, messageRenderer, arguments.verbose);
|
||||||
|
Disposable rootDisposable = CompileEnvironmentUtil.createMockDisposable();
|
||||||
|
|
||||||
|
JetCoreEnvironment environment = JetCoreEnvironment.getCoreEnvironmentForJVM(rootDisposable, dependencies);
|
||||||
|
CompileEnvironmentConfiguration configuration =
|
||||||
|
new CompileEnvironmentConfiguration(environment, dependencies, messageCollector);
|
||||||
|
|
||||||
|
messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment",
|
||||||
|
CompilerMessageLocation.NO_LOCATION);
|
||||||
try {
|
try {
|
||||||
if (arguments.version) {
|
configureEnvironment(configuration, arguments);
|
||||||
errStream.println(messageRenderer
|
|
||||||
.render(CompilerMessageSeverity.INFO, "Kotlin Compiler version " + K2JVMCompilerVersion.VERSION,
|
|
||||||
CompilerMessageLocation.NO_LOCATION));
|
|
||||||
}
|
|
||||||
|
|
||||||
CompilerSpecialMode mode = parseCompilerSpecialMode(arguments);
|
boolean noErrors;
|
||||||
|
if (arguments.module != null) {
|
||||||
File jdkHeadersJar;
|
List<Module> modules = CompileEnvironmentUtil
|
||||||
if (mode.includeJdkHeaders()) {
|
.loadModuleScript(arguments.module, new PrintingMessageCollector(errStream, messageRenderer, false));
|
||||||
if (arguments.jdkHeaders != null) {
|
File directory = new File(arguments.module).getParentFile();
|
||||||
jdkHeadersJar = new File(arguments.jdkHeaders);
|
noErrors = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules,
|
||||||
}
|
directory, arguments.jar, arguments.outputDir,
|
||||||
else {
|
arguments.includeRuntime);
|
||||||
jdkHeadersJar = PathUtil.getAltHeadersPath();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
jdkHeadersJar = null;
|
// TODO ideally we'd unify to just having a single field that supports multiple files/dirs
|
||||||
}
|
if (arguments.getSourceDirs() != null) {
|
||||||
File runtimeJar;
|
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSourceDirectories(configuration,
|
||||||
|
arguments.getSourceDirs(), arguments.jar,
|
||||||
if (mode.includeKotlinRuntime()) {
|
arguments.outputDir,
|
||||||
if (arguments.stdlib != null) {
|
arguments.includeRuntime);
|
||||||
runtimeJar = new File(arguments.stdlib);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
runtimeJar = PathUtil.getDefaultRuntimePath();
|
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration,
|
||||||
|
arguments.src, arguments.jar, arguments.outputDir,
|
||||||
|
arguments.includeRuntime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
return noErrors ? OK : COMPILATION_ERROR;
|
||||||
runtimeJar = null;
|
}
|
||||||
}
|
catch (CompilationException e) {
|
||||||
|
messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e),
|
||||||
CompilerDependencies dependencies = new CompilerDependencies(mode, CompilerDependencies.findRtJar(), jdkHeadersJar, runtimeJar);
|
MessageUtil.psiElementToMessageLocation(e.getElement()));
|
||||||
PrintingMessageCollector messageCollector = new PrintingMessageCollector(errStream, messageRenderer, arguments.verbose);
|
return INTERNAL_ERROR;
|
||||||
Disposable rootDisposable = CompileEnvironmentUtil.createMockDisposable();
|
}
|
||||||
|
catch (Throwable t) {
|
||||||
JetCoreEnvironment environment = JetCoreEnvironment.getCoreEnvironmentForJVM(rootDisposable, dependencies);
|
messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(t),
|
||||||
CompileEnvironmentConfiguration configuration =
|
|
||||||
new CompileEnvironmentConfiguration(environment, dependencies, messageCollector);
|
|
||||||
|
|
||||||
messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment",
|
|
||||||
CompilerMessageLocation.NO_LOCATION);
|
CompilerMessageLocation.NO_LOCATION);
|
||||||
try {
|
return INTERNAL_ERROR;
|
||||||
configureEnvironment(configuration, arguments);
|
|
||||||
|
|
||||||
boolean noErrors;
|
|
||||||
if (arguments.module != null) {
|
|
||||||
List<Module> modules = CompileEnvironmentUtil
|
|
||||||
.loadModuleScript(arguments.module, new PrintingMessageCollector(errStream, messageRenderer, false));
|
|
||||||
File directory = new File(arguments.module).getParentFile();
|
|
||||||
noErrors = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules,
|
|
||||||
directory, arguments.jar, arguments.outputDir,
|
|
||||||
arguments.includeRuntime);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// TODO ideally we'd unify to just having a single field that supports multiple files/dirs
|
|
||||||
if (arguments.getSourceDirs() != null) {
|
|
||||||
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSourceDirectories(configuration,
|
|
||||||
arguments.getSourceDirs(), arguments.jar,
|
|
||||||
arguments.outputDir,
|
|
||||||
arguments.includeRuntime);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration,
|
|
||||||
arguments.src, arguments.jar, arguments.outputDir,
|
|
||||||
arguments.includeRuntime);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return noErrors ? OK : COMPILATION_ERROR;
|
|
||||||
}
|
|
||||||
catch (CompilationException e) {
|
|
||||||
messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e),
|
|
||||||
MessageUtil.psiElementToMessageLocation(e.getElement()));
|
|
||||||
return INTERNAL_ERROR;
|
|
||||||
}
|
|
||||||
catch (Throwable t) {
|
|
||||||
messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(t),
|
|
||||||
CompilerMessageLocation.NO_LOCATION);
|
|
||||||
return INTERNAL_ERROR;
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
Disposer.dispose(rootDisposable);
|
|
||||||
messageCollector.printToErrStream();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
errStream.print(messageRenderer.renderConclusion());
|
Disposer.dispose(rootDisposable);
|
||||||
|
messageCollector.printToErrStream();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,6 +166,15 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, CompileEn
|
|||||||
return new K2JVMCompilerArguments();
|
return new K2JVMCompilerArguments();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: Hacked! Be sure that our kotlin stuff builds correctly before you remove.
|
||||||
|
// our compiler throws method not found error
|
||||||
|
// probably relates to KT-1863... well, may be not
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public ExitCode exec(PrintStream errStream, K2JVMCompilerArguments arguments) {
|
||||||
|
return super.exec(errStream, arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configureEnvironment(@NotNull CompileEnvironmentConfiguration configuration,
|
protected void configureEnvironment(@NotNull CompileEnvironmentConfiguration configuration,
|
||||||
|
|||||||
Reference in New Issue
Block a user