KotlinCompiler -stubs option
* do not fail on errors * do not analyze function bodies * generate "throw new RuntimeException()" in bytecode
This commit is contained in:
@@ -69,8 +69,8 @@ public class KotlinCompiler {
|
||||
@Argument(value = "help", alias = "h", description = "show help")
|
||||
public boolean help;
|
||||
|
||||
@Argument(value = "ignoreErrors", description = "Emit byte code even if there are compilation errors (not recommended)")
|
||||
public boolean ignoreErrors;
|
||||
@Argument(value = "stubs", description = "Compile stubs: ignore function bodies")
|
||||
public boolean stubs;
|
||||
|
||||
@Argument(value = "transformNamesToJava", description = "Transform Kotlin file names to *.java. This option is needed for compiling kotlinized Java library headers")
|
||||
public boolean transformNamesToJava;
|
||||
@@ -119,14 +119,10 @@ public class KotlinCompiler {
|
||||
|
||||
CompileEnvironment environment = new CompileEnvironment(arguments.transformNamesToJava ? ANY_EXTENSION_TO_JAVA : FileNameTransformer.IDENTITY);
|
||||
try {
|
||||
environment.setIgnoreErrors(arguments.ignoreErrors);
|
||||
if (arguments.ignoreErrors) {
|
||||
// To avoid outputting error messages
|
||||
environment.setErrorStream(new PrintStream(new ByteArrayOutputStream()));
|
||||
}
|
||||
else {
|
||||
environment.setErrorStream(errStream);
|
||||
}
|
||||
environment.setIgnoreErrors(false);
|
||||
environment.setErrorStream(errStream);
|
||||
|
||||
environment.setStubs(arguments.stubs);
|
||||
|
||||
if (arguments.docOutputDir != null) {
|
||||
KDocLoader factory = new KDocLoader(arguments.docOutputDir);
|
||||
|
||||
@@ -60,6 +60,7 @@ public class CompileEnvironment {
|
||||
private URL myStdlib;
|
||||
|
||||
private boolean ignoreErrors = false;
|
||||
private boolean stubs = false;
|
||||
|
||||
public CompileEnvironment() {
|
||||
this(FileNameTransformer.IDENTITY);
|
||||
@@ -83,6 +84,10 @@ public class CompileEnvironment {
|
||||
this.ignoreErrors = ignoreErrors;
|
||||
}
|
||||
|
||||
public void setStubs(boolean stubs) {
|
||||
this.stubs = stubs;
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
Disposer.dispose(myRootDisposable);
|
||||
}
|
||||
@@ -229,6 +234,7 @@ public class CompileEnvironment {
|
||||
|
||||
public ClassFileFactory compileModule(Module moduleBuilder, String directory) {
|
||||
CompileSession moduleCompileSession = new CompileSession(myEnvironment);
|
||||
moduleCompileSession.setStubs(stubs);
|
||||
|
||||
if (moduleBuilder.getSourceFiles().isEmpty()) {
|
||||
throw new CompileEnvironmentException("No source files where defined");
|
||||
@@ -349,6 +355,8 @@ public class CompileEnvironment {
|
||||
|
||||
public boolean compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) {
|
||||
CompileSession session = new CompileSession(myEnvironment, myFileNameTransformer);
|
||||
session.setStubs(stubs);
|
||||
|
||||
session.addSources(sourceFileOrDir);
|
||||
|
||||
String mainClass = null;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.jet.compiler;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
@@ -56,6 +57,7 @@ public class CompileSession {
|
||||
private final List<JetFile> mySourceFiles = new ArrayList<JetFile>();
|
||||
private final FileNameTransformer myFileNameTransformer;
|
||||
private List<String> myErrors = new ArrayList<String>();
|
||||
private boolean stubs = false;
|
||||
|
||||
public BindingContext getMyBindingContext() {
|
||||
return myBindingContext;
|
||||
@@ -71,7 +73,11 @@ public class CompileSession {
|
||||
myEnvironment = environment;
|
||||
myFileNameTransformer = fileNameTransformer;
|
||||
}
|
||||
|
||||
|
||||
public void setStubs(boolean stubs) {
|
||||
this.stubs = stubs;
|
||||
}
|
||||
|
||||
public void addSources(String path) {
|
||||
if(path == null)
|
||||
return;
|
||||
@@ -172,8 +178,10 @@ public class CompileSession {
|
||||
}
|
||||
|
||||
private void analyzeAndReportSemanticErrors(ErrorCollector errorCollector) {
|
||||
Predicate<PsiFile> filesToAnalyzeCompletely =
|
||||
stubs ? Predicates.<PsiFile>alwaysFalse() : Predicates.<PsiFile>alwaysTrue();
|
||||
myBindingContext = AnalyzerFacade.analyzeFilesWithJavaIntegration(
|
||||
myEnvironment.getProject(), mySourceFiles, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
|
||||
myEnvironment.getProject(), mySourceFiles, filesToAnalyzeCompletely, JetControlFlowDataTraceFactory.EMPTY);
|
||||
|
||||
for (Diagnostic diagnostic : myBindingContext.getDiagnostics()) {
|
||||
errorCollector.report(diagnostic);
|
||||
@@ -197,7 +205,7 @@ public class CompileSession {
|
||||
@NotNull
|
||||
public ClassFileFactory generate() {
|
||||
Project project = myEnvironment.getProject();
|
||||
GenerationState generationState = new GenerationState(project, ClassBuilderFactory.BINARIES, myFileNameTransformer);
|
||||
GenerationState generationState = new GenerationState(project, ClassBuilderFactories.binaries(stubs), myFileNameTransformer);
|
||||
generationState.compileCorrectFiles(myBindingContext, mySourceFiles, CompilationErrorHandler.THROW_EXCEPTION, true);
|
||||
ClassFileFactory answer = generationState.getFactory();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user