Finally drop attaching stdlib sources everywhere
This commit is contained in:
@@ -19,16 +19,22 @@ public class KotlinCompiler {
|
||||
public static class Arguments {
|
||||
@Argument(value = "output", description = "output directory")
|
||||
public String outputDir;
|
||||
|
||||
@Argument(value = "jar", description = "jar file name")
|
||||
public String jar;
|
||||
|
||||
@Argument(value = "src", description = "source file or directory")
|
||||
public String src;
|
||||
|
||||
@Argument(value = "module", description = "module to compile")
|
||||
public String module;
|
||||
|
||||
@Argument(value = "includeRuntime", description = "include Kotlin runtime in to resulting jar")
|
||||
public boolean includeRuntime;
|
||||
@Argument(value = "excludeStdlib", description = "do not load stdlib (to compile stdlib itself)")
|
||||
public boolean excludeStdlib;
|
||||
|
||||
@Argument(value = "stdlib", description = "Path to the stdlib.jar")
|
||||
public String stdlib;
|
||||
|
||||
@Argument(value = "help", alias = "h", description = "show help")
|
||||
public boolean help;
|
||||
}
|
||||
@@ -61,20 +67,17 @@ public class KotlinCompiler {
|
||||
|
||||
CompileEnvironment environment = new CompileEnvironment();
|
||||
|
||||
try {
|
||||
environment.setJavaRuntime(CompileEnvironment.findRtJar());
|
||||
if (!environment.initializeKotlinRuntime()) {
|
||||
System.err.println("No Kotlin runtime library found");
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
if (arguments.stdlib != null) {
|
||||
environment.setStdlib(arguments.stdlib);
|
||||
}
|
||||
|
||||
try {
|
||||
if (arguments.module != null) {
|
||||
environment.compileModuleScript(arguments.module, arguments.jar, arguments.includeRuntime);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
if (!environment.compileBunchOfSources(arguments.src, arguments.jar, arguments.outputDir, arguments.includeRuntime, !arguments.excludeStdlib)) {
|
||||
if (!environment.compileBunchOfSources(arguments.src, arguments.jar, arguments.outputDir, arguments.includeRuntime)) {
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package org.jetbrains.jet.compiler;
|
||||
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.util.Processor;
|
||||
import jet.modules.AllModules;
|
||||
import jet.modules.Module;
|
||||
@@ -16,7 +19,9 @@ import org.jetbrains.jet.plugin.JetMainDetector;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.List;
|
||||
import java.util.jar.*;
|
||||
|
||||
@@ -29,6 +34,7 @@ public class CompileEnvironment {
|
||||
private JetCoreEnvironment myEnvironment;
|
||||
private final Disposable myRootDisposable;
|
||||
private PrintStream myErrorStream = System.out;
|
||||
private URL myStdlib;
|
||||
|
||||
public CompileEnvironment() {
|
||||
myRootDisposable = new Disposable() {
|
||||
@@ -47,27 +53,6 @@ public class CompileEnvironment {
|
||||
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")) {
|
||||
@@ -85,10 +70,25 @@ public class CompileEnvironment {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setJavaRuntime(File rtJarPath) {
|
||||
myEnvironment.addToClasspath(rtJarPath);
|
||||
public void ensureRuntime() {
|
||||
ensureRuntime(myEnvironment);
|
||||
}
|
||||
|
||||
|
||||
public static void ensureRuntime(JetCoreEnvironment env) {
|
||||
Project project = env.getProject();
|
||||
if (JavaPsiFacade.getInstance(project).findClass("java.lang.Obect", GlobalSearchScope.allScope(project)) == null) {
|
||||
// TODO: prepend
|
||||
env.addToClasspath(findRtJar());
|
||||
}
|
||||
|
||||
if (JavaPsiFacade.getInstance(project).findClass("jet.JetObject", GlobalSearchScope.allScope(project)) == null) {
|
||||
// TODO: prepend
|
||||
File kotlin = getUnpackedRuntimePath();
|
||||
if (kotlin == null) kotlin = getRuntimeJarPath();
|
||||
env.addToClasspath(kotlin);
|
||||
}
|
||||
}
|
||||
|
||||
public static File findRtJar() {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
if ("jre".equals(new File(javaHome).getName())) {
|
||||
@@ -139,7 +139,7 @@ public class CompileEnvironment {
|
||||
public List<Module> loadModuleScript(String moduleFile) {
|
||||
CompileSession scriptCompileSession = new CompileSession(myEnvironment);
|
||||
scriptCompileSession.addSources(moduleFile);
|
||||
scriptCompileSession.addStdLibSources(true);
|
||||
ensureRuntime();
|
||||
|
||||
if (!scriptCompileSession.analyze(myErrorStream)) {
|
||||
return null;
|
||||
@@ -149,8 +149,8 @@ public class CompileEnvironment {
|
||||
return runDefineModules(moduleFile, factory);
|
||||
}
|
||||
|
||||
private static List<Module> runDefineModules(String moduleFile, ClassFileFactory factory) {
|
||||
GeneratedClassLoader loader = new GeneratedClassLoader(factory);
|
||||
private List<Module> runDefineModules(String moduleFile, ClassFileFactory factory) {
|
||||
ClassLoader loader = myStdlib != null ? new GeneratedClassLoader(factory, new URLClassLoader(new URL[] {myStdlib})) : new GeneratedClassLoader(factory);
|
||||
try {
|
||||
Class namespaceClass = loader.loadClass(JvmAbi.PACKAGE_CLASS);
|
||||
final Method method = namespaceClass.getDeclaredMethod("project");
|
||||
@@ -163,16 +163,12 @@ public class CompileEnvironment {
|
||||
|
||||
return AllModules.modules;
|
||||
} catch (Exception e) {
|
||||
throw new CompileEnvironmentException(e);
|
||||
throw new ModuleExecutionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public ClassFileFactory compileModule(Module moduleBuilder, String directory) {
|
||||
CompileSession moduleCompileSession = new CompileSession(myEnvironment);
|
||||
if (!"stdlib".equals(moduleBuilder.getModuleName())) {
|
||||
moduleCompileSession.addStdLibSources(false);
|
||||
}
|
||||
|
||||
for (String sourceFile : moduleBuilder.getSourceFiles()) {
|
||||
File source = new File(sourceFile);
|
||||
if (!source.isAbsolute()) {
|
||||
@@ -182,9 +178,11 @@ public class CompileEnvironment {
|
||||
moduleCompileSession.addSources(source.getPath());
|
||||
}
|
||||
for (String classpathRoot : moduleBuilder.getClasspathRoots()) {
|
||||
if (classpathRoot.contains("/stdlib/") || classpathRoot.contains("\\stdlib\\")) continue; // TODO: We have source-level dependency on stdlib for now
|
||||
myEnvironment.addToClasspath(new File(classpathRoot));
|
||||
}
|
||||
|
||||
ensureRuntime();
|
||||
|
||||
if (!moduleCompileSession.analyze(myErrorStream)) {
|
||||
return null;
|
||||
}
|
||||
@@ -268,12 +266,9 @@ public class CompileEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime, boolean includeSources) {
|
||||
public boolean compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) {
|
||||
CompileSession session = new CompileSession(myEnvironment);
|
||||
session.addSources(sourceFileOrDir);
|
||||
if (includeSources) {
|
||||
session.addStdLibSources(false);
|
||||
}
|
||||
|
||||
String mainClass = null;
|
||||
for (JetFile file : session.getSourceFileNamespaces()) {
|
||||
@@ -282,6 +277,9 @@ public class CompileEnvironment {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ensureRuntime();
|
||||
|
||||
if (!session.analyze(myErrorStream)) {
|
||||
return false;
|
||||
}
|
||||
@@ -314,4 +312,20 @@ public class CompileEnvironment {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setStdlib(String stdlib) {
|
||||
File path = new File(stdlib);
|
||||
if (!path.exists()) {
|
||||
throw new CompileEnvironmentException("'" + stdlib + "' does not exist");
|
||||
}
|
||||
|
||||
try {
|
||||
myStdlib = path.toURL();
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
myEnvironment.addToClasspath(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||
import org.jetbrains.jet.codegen.ClassFileFactory;
|
||||
import org.jetbrains.jet.codegen.GenerationState;
|
||||
@@ -125,6 +126,7 @@ public class CompileSession {
|
||||
return generationState.createText();
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
public boolean addStdLibSources(boolean toModuleSources) {
|
||||
final File unpackedRuntimePath = CompileEnvironment.getUnpackedRuntimePath();
|
||||
if (unpackedRuntimePath != null) {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.jetbrains.jet.compiler;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class ModuleExecutionException extends RuntimeException {
|
||||
public ModuleExecutionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ModuleExecutionException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public ModuleExecutionException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user