Introducing KotlinPaths to impose some discipline on compiler/library location
This commit is contained in:
@@ -36,6 +36,7 @@ import org.jetbrains.jet.codegen.CompilationException;
|
||||
import org.jetbrains.jet.config.CommonConfigurationKeys;
|
||||
import org.jetbrains.jet.config.CompilerConfiguration;
|
||||
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
|
||||
import org.jetbrains.jet.utils.KotlinPaths;
|
||||
import org.jetbrains.jet.utils.PathUtil;
|
||||
|
||||
import java.io.File;
|
||||
@@ -60,9 +61,10 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
|
||||
@Override
|
||||
@NotNull
|
||||
protected ExitCode doExecute(K2JVMCompilerArguments arguments, PrintingMessageCollector messageCollector, Disposable rootDisposable) {
|
||||
KotlinPaths paths = PathUtil.getKotlinPathsForCompiler();
|
||||
CompilerConfiguration configuration = new CompilerConfiguration();
|
||||
configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, getClasspath(arguments));
|
||||
configuration.addAll(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, getAnnotationsPath(arguments));
|
||||
configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, getClasspath(paths, arguments));
|
||||
configuration.addAll(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, getAnnotationsPath(paths, arguments));
|
||||
|
||||
final List<String> argumentsSourceDirs = arguments.getSourceDirs();
|
||||
if (!arguments.script &&
|
||||
@@ -124,7 +126,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
|
||||
if (arguments.module != null) {
|
||||
boolean oldVerbose = messageCollector.isVerbose();
|
||||
messageCollector.setVerbose(false);
|
||||
List<Module> modules = CompileEnvironmentUtil.loadModuleScript(arguments.module, messageCollector);
|
||||
List<Module> modules = CompileEnvironmentUtil.loadModuleScript(paths, arguments.module, messageCollector);
|
||||
messageCollector.setVerbose(oldVerbose);
|
||||
File directory = new File(arguments.module).getParentFile();
|
||||
noErrors = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules,
|
||||
@@ -134,7 +136,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
|
||||
else if (arguments.script) {
|
||||
List<String> scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size());
|
||||
JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, configuration);
|
||||
noErrors = KotlinToJVMBytecodeCompiler.compileAndExecuteScript(environment, scriptArgs);
|
||||
noErrors = KotlinToJVMBytecodeCompiler.compileAndExecuteScript(paths, environment, scriptArgs);
|
||||
}
|
||||
else {
|
||||
JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, configuration);
|
||||
@@ -180,13 +182,13 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<File> getClasspath(@NotNull K2JVMCompilerArguments arguments) {
|
||||
private static List<File> getClasspath(@NotNull KotlinPaths paths, @NotNull K2JVMCompilerArguments arguments) {
|
||||
List<File> classpath = Lists.newArrayList();
|
||||
if (!arguments.noJdk) {
|
||||
classpath.add(PathUtil.findRtJar());
|
||||
}
|
||||
if (!arguments.noStdlib) {
|
||||
classpath.add(PathUtil.getDefaultRuntimePath());
|
||||
classpath.add(paths.getRuntimePath());
|
||||
}
|
||||
if (arguments.classpath != null) {
|
||||
for (String element : Splitter.on(File.pathSeparatorChar).split(arguments.classpath)) {
|
||||
@@ -197,10 +199,10 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<File> getAnnotationsPath(@NotNull K2JVMCompilerArguments arguments) {
|
||||
private static List<File> getAnnotationsPath(@NotNull KotlinPaths paths, @NotNull K2JVMCompilerArguments arguments) {
|
||||
List<File> annotationsPath = Lists.newArrayList();
|
||||
if (!arguments.noJdkAnnotations) {
|
||||
annotationsPath.add(PathUtil.getJdkAnnotationsPath());
|
||||
annotationsPath.add(paths.getJdkAnnotationsPath());
|
||||
}
|
||||
if (arguments.annotations != null) {
|
||||
for (String element : Splitter.on(File.pathSeparatorChar).split(arguments.annotations)) {
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.jetbrains.jet.config.CommonConfigurationKeys;
|
||||
import org.jetbrains.jet.config.CompilerConfiguration;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.jet.utils.KotlinPaths;
|
||||
import org.jetbrains.jet.utils.PathUtil;
|
||||
|
||||
import java.io.File;
|
||||
@@ -81,7 +82,7 @@ public class CompileEnvironmentUtil {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<Module> loadModuleScript(String moduleScriptFile, MessageCollector messageCollector) {
|
||||
public static List<Module> loadModuleScript(KotlinPaths paths, String moduleScriptFile, MessageCollector messageCollector) {
|
||||
Disposable disposable = new Disposable() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
@@ -89,13 +90,13 @@ public class CompileEnvironmentUtil {
|
||||
}
|
||||
};
|
||||
CompilerConfiguration configuration = new CompilerConfiguration();
|
||||
File defaultRuntimePath = PathUtil.getDefaultRuntimePath();
|
||||
if (defaultRuntimePath != null) {
|
||||
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, defaultRuntimePath);
|
||||
File runtimePath = paths.getRuntimePath();
|
||||
if (runtimePath.exists()) {
|
||||
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, runtimePath);
|
||||
}
|
||||
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, PathUtil.findRtJar());
|
||||
File jdkAnnotationsPath = PathUtil.getJdkAnnotationsPath();
|
||||
if (jdkAnnotationsPath != null) {
|
||||
File jdkAnnotationsPath = paths.getJdkAnnotationsPath();
|
||||
if (jdkAnnotationsPath.exists()) {
|
||||
configuration.add(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, jdkAnnotationsPath);
|
||||
}
|
||||
configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, moduleScriptFile);
|
||||
@@ -109,7 +110,7 @@ public class CompileEnvironmentUtil {
|
||||
loadModuleScriptText(moduleScriptFile));
|
||||
}
|
||||
|
||||
List<Module> modules = runDefineModules(moduleScriptFile, generationState.getFactory());
|
||||
List<Module> modules = runDefineModules(paths, moduleScriptFile, generationState.getFactory());
|
||||
|
||||
Disposer.dispose(disposable);
|
||||
|
||||
@@ -123,10 +124,10 @@ public class CompileEnvironmentUtil {
|
||||
return modules;
|
||||
}
|
||||
|
||||
private static List<Module> runDefineModules(String moduleFile, ClassFileFactory factory) {
|
||||
File stdlibJar = PathUtil.getDefaultRuntimePath();
|
||||
private static List<Module> runDefineModules(KotlinPaths paths, String moduleFile, ClassFileFactory factory) {
|
||||
File stdlibJar = paths.getRuntimePath();
|
||||
GeneratedClassLoader loader;
|
||||
if (stdlibJar != null) {
|
||||
if (stdlibJar.exists()) {
|
||||
try {
|
||||
loader = new GeneratedClassLoader(factory, new URLClassLoader(new URL[]{stdlibJar.toURI().toURL()},
|
||||
AllModules.class.getClassLoader()));
|
||||
|
||||
+11
-8
@@ -51,6 +51,7 @@ import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.plugin.JetMainDetector;
|
||||
import org.jetbrains.jet.utils.ExceptionUtils;
|
||||
import org.jetbrains.jet.utils.KotlinPaths;
|
||||
import org.jetbrains.jet.utils.PathUtil;
|
||||
|
||||
import java.io.File;
|
||||
@@ -222,9 +223,10 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
}
|
||||
|
||||
public static boolean compileAndExecuteScript(
|
||||
@NotNull KotlinPaths paths,
|
||||
@NotNull JetCoreEnvironment environment,
|
||||
@NotNull List<String> scriptArgs) {
|
||||
Class<?> scriptClass = compileScript(environment, null);
|
||||
Class<?> scriptClass = compileScript(paths, environment, null);
|
||||
if(scriptClass == null)
|
||||
return false;
|
||||
|
||||
@@ -240,8 +242,8 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Class<?> compileScript(
|
||||
@NotNull JetCoreEnvironment environment, ClassLoader parentLoader) {
|
||||
private static Class<?> compileScript(
|
||||
@NotNull KotlinPaths paths, @NotNull JetCoreEnvironment environment, @Nullable ClassLoader parentLoader) {
|
||||
|
||||
GenerationState generationState = analyzeAndGenerate(environment);
|
||||
if (generationState == null) {
|
||||
@@ -253,7 +255,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
try {
|
||||
GeneratedClassLoader classLoader = new GeneratedClassLoader(factory, new URLClassLoader(new URL[]{
|
||||
// TODO: add all classpath
|
||||
PathUtil.getDefaultRuntimePath().toURI().toURL()
|
||||
paths.getRuntimePath().toURI().toURL()
|
||||
},
|
||||
parentLoader == null ? AllModules.class.getClassLoader() : parentLoader));
|
||||
JetFile scriptFile = environment.getSourceFiles().get(0);
|
||||
@@ -364,8 +366,9 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
}
|
||||
|
||||
public static Class compileScript(
|
||||
ClassLoader parentLoader,
|
||||
String scriptPath,
|
||||
@NotNull ClassLoader parentLoader,
|
||||
@NotNull KotlinPaths paths,
|
||||
@NotNull String scriptPath,
|
||||
@Nullable List<AnalyzerScriptParameter> scriptParameters,
|
||||
@Nullable List<JetScriptDefinition> scriptDefinitions) {
|
||||
final MessageRenderer messageRenderer = MessageRenderer.PLAIN;
|
||||
@@ -377,7 +380,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
compilerConfiguration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, getClasspath(parentLoader));
|
||||
compilerConfiguration.add(JVMConfigurationKeys.CLASSPATH_KEY, PathUtil.findRtJar());
|
||||
compilerConfiguration.addAll(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, Collections.singletonList(
|
||||
PathUtil.getJdkAnnotationsPath()));
|
||||
paths.getJdkAnnotationsPath()));
|
||||
compilerConfiguration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, scriptPath);
|
||||
compilerConfiguration.addAll(CommonConfigurationKeys.SCRIPT_DEFINITIONS_KEY,
|
||||
scriptDefinitions != null ? scriptDefinitions : Collections.<JetScriptDefinition>emptyList());
|
||||
@@ -387,7 +390,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
|
||||
try {
|
||||
JetScriptDefinitionProvider.getInstance(environment.getProject()).markFileAsScript(environment.getSourceFiles().get(0));
|
||||
return compileScript(environment, parentLoader);
|
||||
return compileScript(paths, environment, parentLoader);
|
||||
}
|
||||
catch (CompilationException e) {
|
||||
messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e),
|
||||
|
||||
Reference in New Issue
Block a user