Reverting problematic commits:
fa500f453dAlex Tkachman Today 19:47725bebc23fAlex Tkachman Today 19:36
This commit is contained in:
+81
-67
@@ -1,16 +1,19 @@
|
||||
package org.jetbrains.jet.compiler;
|
||||
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFileSystem;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.Processor;
|
||||
import jet.ExtensionFunction0;
|
||||
import jet.ExtensionFunction10;
|
||||
import jet.Function1;
|
||||
import jet.modules.IModuleBuilder;
|
||||
import jet.modules.IModuleSetBuilder;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.JetCoreEnvironment;
|
||||
import org.jetbrains.jet.codegen.ClassFileFactory;
|
||||
import org.jetbrains.jet.codegen.GeneratedClassLoader;
|
||||
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||
@@ -19,6 +22,7 @@ import org.jetbrains.jet.plugin.JetMainDetector;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
@@ -26,13 +30,74 @@ import java.util.List;
|
||||
import java.util.jar.*;
|
||||
|
||||
/**
|
||||
* @author alex.tkachman
|
||||
* The environment for compiling a bunch of source files or
|
||||
*
|
||||
* @author yole
|
||||
*/
|
||||
public abstract class AbstractCompileEnvironment {
|
||||
protected Project project;
|
||||
public class CompileEnvironment {
|
||||
private JetCoreEnvironment myEnvironment;
|
||||
private final Disposable myRootDisposable;
|
||||
private PrintStream myErrorStream = System.out;
|
||||
|
||||
protected abstract void addToClasspath(File file);
|
||||
public CompileEnvironment() {
|
||||
myRootDisposable = new Disposable() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
}
|
||||
};
|
||||
myEnvironment = new JetCoreEnvironment(myRootDisposable);
|
||||
}
|
||||
|
||||
public void setErrorStream(PrintStream errorStream) {
|
||||
myErrorStream = errorStream;
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
Disposer.dispose(myRootDisposable);
|
||||
}
|
||||
|
||||
public boolean initializeKotlinRuntime() {
|
||||
return initializeKotlinRuntime(myEnvironment);
|
||||
}
|
||||
|
||||
public static boolean initializeKotlinRuntime(JetCoreEnvironment environment) {
|
||||
final File unpackedRuntimePath = getUnpackedRuntimePath();
|
||||
if (unpackedRuntimePath != null) {
|
||||
environment.addToClasspath(unpackedRuntimePath);
|
||||
}
|
||||
else {
|
||||
final File runtimeJarPath = getRuntimeJarPath();
|
||||
if (runtimeJarPath != null && runtimeJarPath.exists()) {
|
||||
environment.addToClasspath(runtimeJarPath);
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static File getUnpackedRuntimePath() {
|
||||
URL url = CompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
|
||||
if (url != null && url.getProtocol().equals("file")) {
|
||||
return new File(url.getPath()).getParentFile().getParentFile();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static File getRuntimeJarPath() {
|
||||
URL url = CompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
|
||||
if (url != null && url.getProtocol().equals("jar")) {
|
||||
String path = url.getPath();
|
||||
return new File(path.substring(path.indexOf(":") + 1, path.indexOf("!/")));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setJavaRuntime(File rtJarPath) {
|
||||
myEnvironment.addToClasspath(rtJarPath);
|
||||
}
|
||||
|
||||
public static File findRtJar(boolean failOnError) {
|
||||
String javaHome = System.getenv("JAVA_HOME");
|
||||
File rtJar;
|
||||
@@ -99,52 +164,6 @@ public abstract class AbstractCompileEnvironment {
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract VirtualFileSystem getLocalFileSystem();
|
||||
|
||||
public abstract VirtualFileSystem getJarFileSystem();
|
||||
|
||||
public Project getProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
public static File getUnpackedRuntimePath() {
|
||||
URL url = CoreCompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
|
||||
if (url != null && url.getProtocol().equals("file")) {
|
||||
return new File(url.getPath()).getParentFile().getParentFile();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static File getRuntimeJarPath() {
|
||||
URL url = CoreCompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
|
||||
if (url != null && url.getProtocol().equals("jar")) {
|
||||
String path = url.getPath();
|
||||
return new File(path.substring(path.indexOf(":") + 1, path.indexOf("!/")));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean initializeKotlinRuntime(AbstractCompileEnvironment environment) {
|
||||
final File unpackedRuntimePath = getUnpackedRuntimePath();
|
||||
if (unpackedRuntimePath != null) {
|
||||
environment.addToClasspath(unpackedRuntimePath);
|
||||
}
|
||||
else {
|
||||
final File runtimeJarPath = getRuntimeJarPath();
|
||||
if (runtimeJarPath != null && runtimeJarPath.exists()) {
|
||||
environment.addToClasspath(runtimeJarPath);
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setJavaRuntime(File rtJarPath) {
|
||||
addToClasspath(rtJarPath);
|
||||
}
|
||||
|
||||
public void compileModuleScript(String moduleFile, String jarPath, boolean jarRuntime) {
|
||||
final IModuleSetBuilder moduleSetBuilder = loadModuleScript(moduleFile);
|
||||
if (moduleSetBuilder == null) {
|
||||
@@ -164,11 +183,11 @@ public abstract class AbstractCompileEnvironment {
|
||||
}
|
||||
|
||||
public IModuleSetBuilder loadModuleScript(String moduleFile) {
|
||||
CompileSession scriptCompileSession = new CompileSession(this);
|
||||
CompileSession scriptCompileSession = new CompileSession(myEnvironment);
|
||||
scriptCompileSession.addSources(moduleFile);
|
||||
scriptCompileSession.addStdLibSources();
|
||||
|
||||
if (!scriptCompileSession.analyze(System.out)) {
|
||||
if (!scriptCompileSession.analyze(myErrorStream)) {
|
||||
return null;
|
||||
}
|
||||
final ClassFileFactory factory = scriptCompileSession.generate();
|
||||
@@ -204,22 +223,22 @@ public abstract class AbstractCompileEnvironment {
|
||||
}
|
||||
|
||||
public ClassFileFactory compileModule(IModuleBuilder moduleBuilder, String directory) {
|
||||
CompileSession moduleCompileSession = new CompileSession(this);
|
||||
CompileSession moduleCompileSession = new CompileSession(myEnvironment);
|
||||
moduleCompileSession.addStdLibSources();
|
||||
for (String sourceFile : moduleBuilder.getSourceFiles()) {
|
||||
moduleCompileSession.addSources(new File(directory, sourceFile).getPath());
|
||||
}
|
||||
for (String classpathRoot : moduleBuilder.getClasspathRoots()) {
|
||||
addToClasspath(new File(classpathRoot));
|
||||
myEnvironment.addToClasspath(new File(classpathRoot));
|
||||
}
|
||||
if (!moduleCompileSession.analyze(System.out)) {
|
||||
if (!moduleCompileSession.analyze(myErrorStream)) {
|
||||
return null;
|
||||
}
|
||||
return moduleCompileSession.generate();
|
||||
}
|
||||
|
||||
private static String getHomeDirectory() {
|
||||
return new File(PathManager.getResourceRoot(CoreCompileEnvironment.class, "/org/jetbrains/jet/compiler/CoreCompileEnvironment.class")).getParentFile().getParentFile().getParent();
|
||||
return new File(PathManager.getResourceRoot(CompileEnvironment.class, "/org/jetbrains/jet/compiler/CompileEnvironment.class")).getParentFile().getParentFile().getParent();
|
||||
}
|
||||
|
||||
private static final List<String> sanitized = Arrays.asList("kotlin/", "std/");
|
||||
@@ -312,11 +331,10 @@ public abstract class AbstractCompileEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
public void compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime, boolean stdlib) {
|
||||
CompileSession session = new CompileSession(this);
|
||||
public void compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) {
|
||||
CompileSession session = new CompileSession(myEnvironment);
|
||||
session.addSources(sourceFileOrDir);
|
||||
if(!stdlib)
|
||||
session.addStdLibSources();
|
||||
session.addStdLibSources();
|
||||
|
||||
String mainClass = null;
|
||||
for (JetNamespace namespace : session.getSourceFileNamespaces()) {
|
||||
@@ -325,7 +343,7 @@ public abstract class AbstractCompileEnvironment {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!session.analyze(System.out)) {
|
||||
if (!session.analyze(myErrorStream)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -358,8 +376,4 @@ public abstract class AbstractCompileEnvironment {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean initializeKotlinRuntime() {
|
||||
return initializeKotlinRuntime(this);
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,11 @@ import com.google.common.base.Predicates;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import org.jetbrains.jet.JetCoreEnvironment;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||
import org.jetbrains.jet.codegen.ClassFileFactory;
|
||||
import org.jetbrains.jet.codegen.CompilationErrorHandler;
|
||||
import org.jetbrains.jet.codegen.GenerationState;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
@@ -19,7 +18,6 @@ import org.jetbrains.jet.plugin.JetFileType;
|
||||
import java.io.File;
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -28,13 +26,13 @@ import java.util.List;
|
||||
* @author yole
|
||||
*/
|
||||
public class CompileSession {
|
||||
private final JetCoreEnvironment myEnvironment;
|
||||
private final List<JetNamespace> mySourceFileNamespaces = new ArrayList<JetNamespace>();
|
||||
private final List<JetNamespace> myLibrarySourceFileNamespaces = new ArrayList<JetNamespace>();
|
||||
private List<String> myErrors = new ArrayList<String>();
|
||||
private BindingContext myBindingContext;
|
||||
private AbstractCompileEnvironment myEnvironment;
|
||||
|
||||
public CompileSession(AbstractCompileEnvironment environment) {
|
||||
public CompileSession(JetCoreEnvironment environment) {
|
||||
myEnvironment = environment;
|
||||
}
|
||||
|
||||
@@ -52,32 +50,28 @@ public class CompileSession {
|
||||
return;
|
||||
}
|
||||
|
||||
addSources(new File(path), mySourceFileNamespaces);
|
||||
addSources(new File(path));
|
||||
}
|
||||
|
||||
private void addSources(File file, List<JetNamespace> namespaces) {
|
||||
private void addSources(File file) {
|
||||
if(file.isDirectory()) {
|
||||
for (File child : file.listFiles()) {
|
||||
addSources(child, namespaces);
|
||||
addSources(child);
|
||||
}
|
||||
}
|
||||
else {
|
||||
VirtualFile fileByPath = myEnvironment.getLocalFileSystem().findFileByPath(file.getAbsolutePath());
|
||||
PsiFile psiFile = PsiManager.getInstance(myEnvironment.getProject()).findFile(fileByPath);
|
||||
if(psiFile instanceof JetFile) {
|
||||
namespaces.add(((JetFile) psiFile).getRootNamespace());
|
||||
mySourceFileNamespaces.add(((JetFile) psiFile).getRootNamespace());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addSources(VirtualFile vFile) {
|
||||
addSources(vFile, mySourceFileNamespaces);
|
||||
}
|
||||
|
||||
public void addSources(VirtualFile vFile, List<JetNamespace> namespaces) {
|
||||
if (vFile.isDirectory()) {
|
||||
for (VirtualFile virtualFile : vFile.getChildren()) {
|
||||
addSources(virtualFile, namespaces);
|
||||
addSources(virtualFile);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -90,28 +84,10 @@ public class CompileSession {
|
||||
}
|
||||
}
|
||||
|
||||
public void addSources(VirtualFile[] allFiles) {
|
||||
for(VirtualFile vFile: allFiles) {
|
||||
if (vFile.getFileType() == JetFileType.INSTANCE) {
|
||||
PsiFile psiFile = PsiManager.getInstance(myEnvironment.getProject()).findFile(vFile);
|
||||
if (psiFile instanceof JetFile) {
|
||||
mySourceFileNamespaces.add(((JetFile) psiFile).getRootNamespace());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<JetNamespace> getSourceFileNamespaces() {
|
||||
return mySourceFileNamespaces;
|
||||
}
|
||||
|
||||
public BindingContext analyze() {
|
||||
List<JetNamespace> allNamespaces = new ArrayList<JetNamespace>(mySourceFileNamespaces);
|
||||
allNamespaces.addAll(myLibrarySourceFileNamespaces);
|
||||
return AnalyzerFacade.analyzeNamespacesWithJavaIntegration(
|
||||
myEnvironment.getProject(), allNamespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
|
||||
}
|
||||
|
||||
public boolean analyze(final PrintStream out) {
|
||||
if (!myErrors.isEmpty()) {
|
||||
for (String error : myErrors) {
|
||||
@@ -119,19 +95,18 @@ public class CompileSession {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
myBindingContext = analyze();
|
||||
List<JetNamespace> allNamespaces = new ArrayList<JetNamespace>(mySourceFileNamespaces);
|
||||
allNamespaces.addAll(myLibrarySourceFileNamespaces);
|
||||
myBindingContext = AnalyzerFacade.analyzeNamespacesWithJavaIntegration(
|
||||
myEnvironment.getProject(), allNamespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
|
||||
ErrorCollector errorCollector = new ErrorCollector(myBindingContext);
|
||||
errorCollector.report(out);
|
||||
return !errorCollector.hasErrors;
|
||||
}
|
||||
|
||||
public ClassFileFactory generate() {
|
||||
return generate(CompilationErrorHandler.THROW_EXCEPTION);
|
||||
}
|
||||
|
||||
public ClassFileFactory generate(CompilationErrorHandler handler) {
|
||||
GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.BINARIES);
|
||||
generationState.compileCorrectNamespaces(myBindingContext, mySourceFileNamespaces, handler);
|
||||
generationState.compileCorrectNamespaces(myBindingContext, mySourceFileNamespaces);
|
||||
return generationState.getFactory();
|
||||
}
|
||||
|
||||
@@ -142,25 +117,16 @@ public class CompileSession {
|
||||
}
|
||||
|
||||
public boolean addStdLibSources() {
|
||||
return addStdLibSources(false);
|
||||
}
|
||||
|
||||
public boolean addStdLibSources(boolean forAnalize) {
|
||||
/*
|
||||
@todo
|
||||
We add sources as library sources in case of jar
|
||||
but as regular one if there is no jar
|
||||
*/
|
||||
final File unpackedRuntimePath = CoreCompileEnvironment.getUnpackedRuntimePath();
|
||||
final File unpackedRuntimePath = CompileEnvironment.getUnpackedRuntimePath();
|
||||
if (unpackedRuntimePath != null) {
|
||||
addSources(new File(unpackedRuntimePath, "stdlib/ktSrc").getAbsoluteFile(), forAnalize ? myLibrarySourceFileNamespaces : mySourceFileNamespaces);
|
||||
addSources(new File(unpackedRuntimePath, "../../../stdlib/ktSrc").getAbsoluteFile());
|
||||
}
|
||||
else {
|
||||
final File runtimeJarPath = CoreCompileEnvironment.getRuntimeJarPath();
|
||||
final File runtimeJarPath = CompileEnvironment.getRuntimeJarPath();
|
||||
if (runtimeJarPath != null && runtimeJarPath.exists()) {
|
||||
VirtualFile runtimeJar = myEnvironment.getLocalFileSystem().findFileByPath(runtimeJarPath.getAbsolutePath());
|
||||
VirtualFile jarRoot = myEnvironment.getJarFileSystem().findFileByPath(runtimeJar.getPath() + "!/stdlib/ktSrc");
|
||||
addSources(jarRoot, myLibrarySourceFileNamespaces);
|
||||
addSources(jarRoot);
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
@@ -168,8 +134,4 @@ public class CompileSession {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<JetNamespace> getLibrarySourceFileNamespaces() {
|
||||
return myLibrarySourceFileNamespaces;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
package org.jetbrains.jet.compiler;
|
||||
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFileSystem;
|
||||
import com.intellij.util.Processor;
|
||||
import jet.ExtensionFunction0;
|
||||
import jet.modules.IModuleBuilder;
|
||||
import jet.modules.IModuleSetBuilder;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.JetCoreEnvironment;
|
||||
import org.jetbrains.jet.codegen.ClassFileFactory;
|
||||
import org.jetbrains.jet.codegen.GeneratedClassLoader;
|
||||
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.plugin.JetMainDetector;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.jar.*;
|
||||
|
||||
/**
|
||||
* The environment for compiling a bunch of source files or
|
||||
*
|
||||
* @author yole
|
||||
* @author alex.tkachman
|
||||
*/
|
||||
public class CoreCompileEnvironment extends AbstractCompileEnvironment {
|
||||
private final Disposable myRootDisposable;
|
||||
private final VirtualFileSystem localFileSystem;
|
||||
private VirtualFileSystem jarFileSystem;
|
||||
private JetCoreEnvironment myEnvironment;
|
||||
|
||||
public CoreCompileEnvironment() {
|
||||
myRootDisposable = new Disposable() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
}
|
||||
};
|
||||
myEnvironment = new JetCoreEnvironment(myRootDisposable);
|
||||
localFileSystem = myEnvironment.getLocalFileSystem();
|
||||
jarFileSystem = myEnvironment.getJarFileSystem();
|
||||
project = myEnvironment.getProject();
|
||||
}
|
||||
|
||||
public CoreCompileEnvironment(JetCoreEnvironment myEnvironment) {
|
||||
this.myRootDisposable = null;
|
||||
this.myEnvironment = myEnvironment;
|
||||
localFileSystem = myEnvironment.getLocalFileSystem();
|
||||
jarFileSystem = myEnvironment.getJarFileSystem();
|
||||
project = myEnvironment.getProject();
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
if(myRootDisposable != null)
|
||||
Disposer.dispose(myRootDisposable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addToClasspath(File file) {
|
||||
myEnvironment.addToClasspath(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VirtualFileSystem getLocalFileSystem() {
|
||||
return localFileSystem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Project getProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VirtualFileSystem getJarFileSystem() {
|
||||
return jarFileSystem;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user