proper environment when compiling special things
* do not include anything when compiling builtins * do not include kotlin-runtime and jdk-headers when compiling jdk-headers
This commit is contained in:
@@ -53,8 +53,8 @@ public class CompilerArguments {
|
||||
@Argument(value = "help", alias = "h", description = "show help")
|
||||
public boolean help;
|
||||
|
||||
@Argument(value = "stubs", description = "Compile stubs: ignore function bodies")
|
||||
public boolean stubs;
|
||||
@Argument(value = "mode", description = "Special compiler modes: stubs or jdkHeaders")
|
||||
public String mode;
|
||||
|
||||
@Argument(value = "tags", description = "Demarcate each compilation message (error, warning, etc) with an open and close tag")
|
||||
public boolean tags;
|
||||
@@ -130,14 +130,6 @@ public class CompilerArguments {
|
||||
this.stdlib = stdlib;
|
||||
}
|
||||
|
||||
public boolean isStubs() {
|
||||
return stubs;
|
||||
}
|
||||
|
||||
public void setStubs(boolean stubs) {
|
||||
this.stubs = stubs;
|
||||
}
|
||||
|
||||
public boolean isTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
@@ -17,11 +17,9 @@
|
||||
package org.jetbrains.jet.cli;
|
||||
|
||||
import com.sampullara.cli.Args;
|
||||
import org.jetbrains.jet.compiler.CompileEnvironment;
|
||||
import org.jetbrains.jet.compiler.CompileEnvironmentException;
|
||||
import org.jetbrains.jet.compiler.CompilerPlugin;
|
||||
import org.jetbrains.jet.compiler.MessageRenderer;
|
||||
import org.jetbrains.jet.compiler.*;
|
||||
import org.jetbrains.jet.lang.diagnostics.Severity;
|
||||
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.List;
|
||||
@@ -99,7 +97,18 @@ public class KotlinCompiler {
|
||||
errStream.println(messageRenderer.render(Severity.INFO, "Kotlin Compiler version " + CompilerVersion.VERSION, null, -1, -1));
|
||||
}
|
||||
|
||||
CompileEnvironment environment = new CompileEnvironment(messageRenderer, arguments.verbose);
|
||||
CompilerSpecialMode mode;
|
||||
if (arguments.mode == null) {
|
||||
mode = CompilerSpecialMode.REGULAR;
|
||||
} else if (arguments.mode.equals("jdkHeaders")) {
|
||||
mode = CompilerSpecialMode.JDK_HEADERS;
|
||||
} else if (arguments.mode.equals("builtins")) {
|
||||
mode = CompilerSpecialMode.BUILTINS;
|
||||
} else {
|
||||
throw new IllegalArgumentException("unknown compiler mode: " + arguments.mode);
|
||||
}
|
||||
|
||||
CompileEnvironment environment = new CompileEnvironment(messageRenderer, arguments.verbose, mode);
|
||||
try {
|
||||
configureEnvironment(environment, arguments, errStream);
|
||||
|
||||
@@ -163,8 +172,6 @@ public class KotlinCompiler {
|
||||
environment.setIgnoreErrors(false);
|
||||
environment.setErrorStream(errStream);
|
||||
|
||||
environment.setStubs(arguments.stubs);
|
||||
|
||||
// install any compiler plugins
|
||||
List<CompilerPlugin> plugins = arguments.getCompilerPlugins();
|
||||
if (plugins != null) {
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.jetbrains.jet.lang.resolve.FqName;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
import org.jetbrains.jet.plugin.JetLanguage;
|
||||
import org.jetbrains.jet.plugin.JetMainDetector;
|
||||
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
|
||||
import org.jetbrains.jet.utils.PathUtil;
|
||||
|
||||
import java.io.*;
|
||||
@@ -62,11 +63,11 @@ public class CompileEnvironment {
|
||||
private URL stdlibUrl;
|
||||
|
||||
private boolean ignoreErrors = false;
|
||||
private boolean stubs = false;
|
||||
private final CompilerSpecialMode mode;
|
||||
private final boolean verbose;
|
||||
|
||||
public CompileEnvironment() {
|
||||
this(MessageRenderer.PLAIN, false);
|
||||
this(MessageRenderer.PLAIN, false, CompilerSpecialMode.REGULAR);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,14 +77,15 @@ public class CompileEnvironment {
|
||||
* @param messageRenderer
|
||||
* @param verbose
|
||||
*/
|
||||
public CompileEnvironment(MessageRenderer messageRenderer, boolean verbose) {
|
||||
public CompileEnvironment(MessageRenderer messageRenderer, boolean verbose, CompilerSpecialMode mode) {
|
||||
this.mode = mode;
|
||||
this.verbose = verbose;
|
||||
rootDisposable = new Disposable() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
}
|
||||
};
|
||||
environment = new JetCoreEnvironment(rootDisposable);
|
||||
environment = new JetCoreEnvironment(rootDisposable, mode == CompilerSpecialMode.REGULAR);
|
||||
this.messageRenderer = messageRenderer;
|
||||
}
|
||||
|
||||
@@ -95,10 +97,6 @@ public class CompileEnvironment {
|
||||
this.ignoreErrors = ignoreErrors;
|
||||
}
|
||||
|
||||
public void setStubs(boolean stubs) {
|
||||
this.stubs = stubs;
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
Disposer.dispose(rootDisposable);
|
||||
}
|
||||
@@ -123,17 +121,24 @@ public class CompileEnvironment {
|
||||
}
|
||||
|
||||
public void ensureRuntime() {
|
||||
ensureRuntime(environment);
|
||||
if (mode == CompilerSpecialMode.REGULAR) {
|
||||
ensureRuntime(environment);
|
||||
} else if (mode == CompilerSpecialMode.JDK_HEADERS) {
|
||||
ensureJdkRuntime(environment);
|
||||
} else if (mode == CompilerSpecialMode.BUILTINS) {
|
||||
// nop
|
||||
} else {
|
||||
throw new IllegalStateException("unknown mode: " + mode);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ensureRuntime(@NotNull JetCoreEnvironment env) {
|
||||
Project project = env.getProject();
|
||||
if (JavaPsiFacade.getInstance(project).findClass("java.lang.Object", GlobalSearchScope.allScope(project)) == null) {
|
||||
// TODO: prepend
|
||||
env.addToClasspath(findRtJar());
|
||||
}
|
||||
ensureJdkRuntime(env);
|
||||
ensureKotlinRuntime(env);
|
||||
}
|
||||
|
||||
if (JavaPsiFacade.getInstance(project).findClass("jet.JetObject", GlobalSearchScope.allScope(project)) == null) {
|
||||
private static void ensureKotlinRuntime(JetCoreEnvironment env) {
|
||||
if (JavaPsiFacade.getInstance(env.getProject()).findClass("jet.JetObject", GlobalSearchScope.allScope(env.getProject())) == null) {
|
||||
// TODO: prepend
|
||||
File kotlin = PathUtil.getDefaultRuntimePath();
|
||||
if (kotlin == null || !kotlin.exists()) {
|
||||
@@ -147,6 +152,13 @@ public class CompileEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
public static void ensureJdkRuntime(JetCoreEnvironment env) {
|
||||
if (JavaPsiFacade.getInstance(env.getProject()).findClass("java.lang.Object", GlobalSearchScope.allScope(env.getProject())) == null) {
|
||||
// TODO: prepend
|
||||
env.addToClasspath(findRtJar());
|
||||
}
|
||||
}
|
||||
|
||||
public static File findRtJar() {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
if ("jre".equals(new File(javaHome).getName())) {
|
||||
@@ -220,7 +232,7 @@ public class CompileEnvironment {
|
||||
}
|
||||
|
||||
private CompileEnvironment copyEnvironment(boolean verbose) {
|
||||
CompileEnvironment compileEnvironment = new CompileEnvironment(messageRenderer, verbose);
|
||||
CompileEnvironment compileEnvironment = new CompileEnvironment(messageRenderer, verbose, mode);
|
||||
compileEnvironment.setIgnoreErrors(ignoreErrors);
|
||||
compileEnvironment.setErrorStream(errorStream);
|
||||
// copy across any compiler plugins
|
||||
@@ -268,7 +280,7 @@ public class CompileEnvironment {
|
||||
|
||||
public ClassFileFactory compileModule(Module moduleBuilder, String directory) {
|
||||
CompileSession moduleCompileSession = newCompileSession();
|
||||
moduleCompileSession.setStubs(stubs);
|
||||
moduleCompileSession.setStubs(mode != CompilerSpecialMode.REGULAR);
|
||||
|
||||
if (moduleBuilder.getSourceFiles().isEmpty()) {
|
||||
throw new CompileEnvironmentException("No source files where defined");
|
||||
@@ -389,7 +401,7 @@ public class CompileEnvironment {
|
||||
|
||||
public boolean compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) {
|
||||
CompileSession session = newCompileSession();
|
||||
session.setStubs(stubs);
|
||||
session.setStubs(mode != CompilerSpecialMode.REGULAR);
|
||||
|
||||
session.addSources(sourceFileOrDir);
|
||||
|
||||
@@ -426,7 +438,7 @@ public class CompileEnvironment {
|
||||
}
|
||||
|
||||
private CompileSession newCompileSession() {
|
||||
CompileSession answer = new CompileSession(environment, messageRenderer, errorStream, verbose);
|
||||
CompileSession answer = new CompileSession(environment, messageRenderer, errorStream, verbose, mode);
|
||||
environment.setSession(answer);
|
||||
return answer;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
|
||||
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
|
||||
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
|
||||
import org.jetbrains.jet.plugin.JetFileType;
|
||||
import org.jetbrains.jet.utils.Progress;
|
||||
@@ -67,13 +68,15 @@ public class CompileSession {
|
||||
private final MessageRenderer messageRenderer;
|
||||
private final PrintStream errorStream;
|
||||
private final boolean isVerbose;
|
||||
private final CompilerSpecialMode compilerSpecialMode;
|
||||
private AnalyzeExhaust bindingContext;
|
||||
|
||||
public CompileSession(JetCoreEnvironment environment, MessageRenderer messageRenderer, PrintStream errorStream, boolean verbose) {
|
||||
public CompileSession(JetCoreEnvironment environment, MessageRenderer messageRenderer, PrintStream errorStream, boolean verbose, CompilerSpecialMode mode) {
|
||||
this.environment = environment;
|
||||
this.messageRenderer = messageRenderer;
|
||||
this.errorStream = errorStream;
|
||||
isVerbose = verbose;
|
||||
this.compilerSpecialMode = mode;
|
||||
messageCollector = new MessageCollector(this.messageRenderer);
|
||||
}
|
||||
|
||||
@@ -173,7 +176,7 @@ public class CompileSession {
|
||||
Predicate<PsiFile> filesToAnalyzeCompletely =
|
||||
stubs ? Predicates.<PsiFile>alwaysFalse() : Predicates.<PsiFile>alwaysTrue();
|
||||
bindingContext = AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
|
||||
environment.getProject(), sourceFiles, filesToAnalyzeCompletely, JetControlFlowDataTraceFactory.EMPTY);
|
||||
environment.getProject(), sourceFiles, filesToAnalyzeCompletely, JetControlFlowDataTraceFactory.EMPTY, compilerSpecialMode);
|
||||
|
||||
for (Diagnostic diagnostic : bindingContext.getBindingContext().getDiagnostics()) {
|
||||
reportDiagnostic(messageCollector, diagnostic);
|
||||
|
||||
@@ -43,7 +43,7 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
|
||||
private List<CompilerPlugin> compilerPlugins = new ArrayList<CompilerPlugin>();
|
||||
private CompileSession session;
|
||||
|
||||
public JetCoreEnvironment(Disposable parentDisposable) {
|
||||
public JetCoreEnvironment(Disposable parentDisposable, boolean includeJdkHeaders) {
|
||||
super(parentDisposable);
|
||||
registerFileType(JetFileType.INSTANCE, "kt");
|
||||
registerFileType(JetFileType.INSTANCE, "kts");
|
||||
@@ -58,8 +58,10 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
|
||||
.getExtensionPoint(PsiElementFinder.EP_NAME)
|
||||
.registerExtension(new JavaElementFinder(myProject));
|
||||
|
||||
for (VirtualFile root : PathUtil.getAltHeadersRoots()) {
|
||||
addLibraryRoot(root);
|
||||
if (includeJdkHeaders) {
|
||||
for (VirtualFile root : PathUtil.getAltHeadersRoots()) {
|
||||
addLibraryRoot(root);
|
||||
}
|
||||
}
|
||||
|
||||
JetStandardLibrary.initialize(getProject());
|
||||
|
||||
Reference in New Issue
Block a user