Made compiler aware of multiple alt-header paths.

This commit is contained in:
Evgeny Gerashchenko
2012-05-15 00:16:44 +04:00
parent 554280a1b3
commit 4ab2588cbd
7 changed files with 62 additions and 25 deletions
@@ -19,6 +19,10 @@ package org.jetbrains.jet.cli.jvm;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import jet.modules.Module;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.CLICompiler;
@@ -36,6 +40,7 @@ import org.jetbrains.jet.utils.PathUtil;
import java.io.File;
import java.io.PrintStream;
import java.util.List;
import java.util.regex.Pattern;
import static org.jetbrains.jet.cli.common.ExitCode.*;
@@ -55,17 +60,18 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
protected ExitCode doExecute(K2JVMCompilerArguments arguments, PrintingMessageCollector messageCollector, Disposable rootDisposable) {
CompilerSpecialMode mode = parseCompilerSpecialMode(arguments);
File jdkHeadersJar;
File[] altHeadersClasspath;
if (mode.includeJdkHeaders()) {
if (arguments.jdkHeaders != null) {
jdkHeadersJar = new File(arguments.jdkHeaders);
File[] defaultAltHeadersPathArray = {PathUtil.getAltHeadersPath()};
if (arguments.altHeaders != null) {
altHeadersClasspath = ArrayUtil.mergeArrays(pathsToFiles(arguments.altHeaders), defaultAltHeadersPathArray);
}
else {
jdkHeadersJar = PathUtil.getAltHeadersPath();
altHeadersClasspath = defaultAltHeadersPathArray;
}
}
else {
jdkHeadersJar = null;
altHeadersClasspath = new File[0];
}
File runtimeJar;
@@ -81,7 +87,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
runtimeJar = null;
}
CompilerDependencies dependencies = new CompilerDependencies(mode, CompilerDependencies.findRtJar(), jdkHeadersJar, runtimeJar);
CompilerDependencies dependencies = new CompilerDependencies(mode, CompilerDependencies.findRtJar(), altHeadersClasspath, runtimeJar);
JetCoreEnvironment environment = JetCoreEnvironment.getCoreEnvironmentForJVM(rootDisposable, dependencies);
K2JVMCompileEnvironmentConfiguration configuration =
new K2JVMCompileEnvironmentConfiguration(environment, messageCollector);
@@ -181,4 +187,14 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
CompileEnvironmentUtil.addToClasspath(configuration.getEnvironment(), Iterables.toArray(classpath, String.class));
}
}
private static File[] pathsToFiles(String paths) {
return ContainerUtil.map(Iterables.toArray(Splitter.on(File.pathSeparatorChar).split(paths), String.class),
new Function<String, File>() {
@Override
public File fun(String s) {
return new File(s);
}
}, new File[0]);
}
}
@@ -54,8 +54,8 @@ public class K2JVMCompilerArguments extends CompilerArguments {
@Argument(value = "stdlib", description = "Path to the stdlib.jar")
public String stdlib;
@Argument(value = "jdkHeaders", description = "Path to the kotlin-jdk-headers.jar")
public String jdkHeaders;
@Argument(value = "altHeaders", description = "Path to the alternative library headers paths")
public String altHeaders;
@Argument(value = "mode", description = "Special compiler modes: stubs or jdkHeaders")
public String mode;
@@ -85,7 +85,7 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
}
if (compilerSpecialMode.includeJdkHeaders()) {
for (VirtualFile root : compilerDependencies.getJdkHeaderRoots()) {
for (VirtualFile root : compilerDependencies.getAltHeaderRoots()) {
addLibraryRoot(root);
}
}
@@ -16,7 +16,11 @@
package org.jetbrains.jet.lang.resolve.java;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.utils.PathUtil;
@@ -34,15 +38,15 @@ public class CompilerDependencies {
private final CompilerSpecialMode compilerSpecialMode;
@Nullable
private final File jdkJar;
@Nullable
private final File jdkHeadersJar;
@NotNull
private final File[] altHeadersClasspath;
@Nullable
private final File runtimeJar;
public CompilerDependencies(@NotNull CompilerSpecialMode compilerSpecialMode, @Nullable File jdkJar, @Nullable File jdkHeadersJar, @Nullable File runtimeJar) {
public CompilerDependencies(@NotNull CompilerSpecialMode compilerSpecialMode, @Nullable File jdkJar, @NotNull File[] altHeadersClasspath, @Nullable File runtimeJar) {
this.compilerSpecialMode = compilerSpecialMode;
this.jdkJar = jdkJar;
this.jdkHeadersJar = jdkHeadersJar;
this.altHeadersClasspath = altHeadersClasspath;
this.runtimeJar = runtimeJar;
if (compilerSpecialMode.includeJdk()) {
@@ -51,8 +55,8 @@ public class CompilerDependencies {
}
}
if (compilerSpecialMode.includeJdkHeaders()) {
if (jdkHeadersJar == null) {
throw new IllegalArgumentException("jdkHeaders must be included for mode " + compilerSpecialMode);
if (altHeadersClasspath.length == 0) {
throw new IllegalArgumentException("altHeaders must be included for mode " + compilerSpecialMode);
}
}
if (compilerSpecialMode.includeKotlinRuntime()) {
@@ -72,9 +76,9 @@ public class CompilerDependencies {
return jdkJar;
}
@Nullable
public File getJdkHeadersJar() {
return jdkHeadersJar;
@NotNull
public File[] getAltHeadersClassPath() {
return altHeadersClasspath;
}
@Nullable
@@ -83,9 +87,24 @@ public class CompilerDependencies {
}
@NotNull
public List<VirtualFile> getJdkHeaderRoots() {
public List<VirtualFile> getAltHeaderRoots() {
if (compilerSpecialMode.includeJdkHeaders()) {
return Collections.singletonList(PathUtil.jarFileToVirtualFile(jdkHeadersJar));
return ContainerUtil.map2List(altHeadersClasspath, new Function<File, VirtualFile>() {
@Override
public VirtualFile fun(File file) {
if (file.exists()) {
if (file.isDirectory()) {
return VirtualFileManager.getInstance().findFileByUrl("file://" + FileUtil.toSystemIndependentName(file.getAbsolutePath()));
}
else {
return PathUtil.jarFileToVirtualFile(file);
}
}
else {
throw new IllegalStateException("Path " + file + " does not exist.");
}
}
});
}
else {
return Collections.emptyList();
@@ -107,7 +126,7 @@ public class CompilerDependencies {
return new CompilerDependencies(
compilerSpecialMode,
compilerSpecialMode.includeJdk() ? findRtJar() : null,
compilerSpecialMode.includeJdkHeaders() ? PathUtil.getAltHeadersPath() : null,
compilerSpecialMode.includeJdkHeaders() ? new File[]{PathUtil.getAltHeadersPath()} : new File[0],
compilerSpecialMode.includeKotlinRuntime() ? PathUtil.getDefaultRuntimePath() : null);
}
@@ -59,7 +59,7 @@ public class PsiClassFinderForJvm implements PsiClassFinder {
@PostConstruct
public void initialize() {
this.altClassFinder = new AltClassFinder(project, compilerDependencies.getJdkHeaderRoots());
this.altClassFinder = new AltClassFinder(project, compilerDependencies.getAltHeaderRoots());
this.javaSearchScope = new DelegatingGlobalSearchScope(GlobalSearchScope.allScope(project)) {
@Override
public boolean contains(VirtualFile file) {
@@ -22,9 +22,10 @@ import org.jetbrains.jet.codegen.forTestCompile.ForTestCompileJdkHeaders;
import org.jetbrains.jet.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.jet.lang.resolve.java.CompilerDependencies;
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
import org.jetbrains.jet.utils.PathUtil;
import org.junit.Test;
import java.io.File;
/**
* @author Stepan Koltsov
*/
@@ -53,7 +54,7 @@ public class CompileCompilerDependenciesTest {
return new CompilerDependencies(
compilerSpecialMode,
compilerSpecialMode.includeJdk() ? (mockJdk ? JetTestUtils.findMockJdkRtJar() : CompilerDependencies.findRtJar()) : null,
compilerSpecialMode.includeJdkHeaders() ? ForTestCompileJdkHeaders.jdkHeadersForTests() : null,
compilerSpecialMode.includeJdkHeaders() ? new File[]{ForTestCompileJdkHeaders.jdkHeadersForTests()} : new File[0],
compilerSpecialMode.includeKotlinRuntime() ? ForTestCompileRuntime.runtimeJarForTests() : null);
}
}
@@ -20,6 +20,7 @@
package org.jetbrains.jet.utils;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import org.jetbrains.annotations.NotNull;
@@ -106,6 +107,6 @@ public class PathUtil {
if (!file.exists() || !file.isFile()) {
throw new IllegalStateException("file must exist and be regular to be converted to virtual file: " + file);
}
return VirtualFileManager.getInstance().findFileByUrl("jar://" + file.getPath() + "!/");
return VirtualFileManager.getInstance().findFileByUrl("jar://" + FileUtil.toSystemIndependentName(file.getAbsolutePath()) + "!/");
}
}