Got rid of creating JetCoreEnvironment which wont be used when compiling modules from module script.

This commit is contained in:
Evgeny Gerashchenko
2012-07-24 17:05:23 +04:00
parent 75ce5b3a1b
commit edb2e0e3b6
3 changed files with 16 additions and 11 deletions
@@ -57,6 +57,12 @@ public class BytecodeCompiler {
* @return compile environment instance * @return compile environment instance
*/ */
private JetCoreEnvironment env(String stdlib, String[] classpath, String[] sourceRoots) { private JetCoreEnvironment env(String stdlib, String[] classpath, String[] sourceRoots) {
CompilerConfiguration configuration = createConfiguration(stdlib, classpath, sourceRoots);
return new JetCoreEnvironment(CompileEnvironmentUtil.createMockDisposable(), configuration);
}
private CompilerConfiguration createConfiguration(String stdlib, String[] classpath, String[] sourceRoots) {
CompilerConfiguration configuration = new CompilerConfiguration(); CompilerConfiguration configuration = new CompilerConfiguration();
configuration.add(CLASSPATH_KEY, PathUtil.findRtJar()); configuration.add(CLASSPATH_KEY, PathUtil.findRtJar());
if ((stdlib != null) && (stdlib.trim().length() > 0)) { if ((stdlib != null) && (stdlib.trim().length() > 0)) {
@@ -83,8 +89,7 @@ public class BytecodeCompiler {
// lets register any compiler plugins // lets register any compiler plugins
configuration.addAll(CLIConfigurationKeys.COMPILER_PLUGINS, getCompilerPlugins()); configuration.addAll(CLIConfigurationKeys.COMPILER_PLUGINS, getCompilerPlugins());
return configuration;
return new JetCoreEnvironment(CompileEnvironmentUtil.createMockDisposable(), configuration);
} }
@@ -173,9 +178,9 @@ public class BytecodeCompiler {
for (Module m : modules) { for (Module m : modules) {
sourcesRoots.addAll(m.getSourceFiles()); sourcesRoots.addAll(m.getSourceFiles());
} }
JetCoreEnvironment env = env(stdlib, classpath, sourcesRoots.toArray(new String[0])); CompilerConfiguration configuration = createConfiguration(stdlib, classpath, sourcesRoots.toArray(new String[0]));
File directory = new File(module).getParentFile(); File directory = new File(module).getParentFile();
boolean success = KotlinToJVMBytecodeCompiler.compileModules(env, modules, directory, new File(jar), null, includeRuntime); boolean success = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules, directory, new File(jar), null, includeRuntime);
if (!success) { if (!success) {
throw new CompileEnvironmentException(errorMessage(module, false)); throw new CompileEnvironmentException(errorMessage(module, false));
} }
@@ -93,7 +93,6 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
} }
} }
JetCoreEnvironment environment = JetCoreEnvironment.createCoreEnvironmentForJVM(rootDisposable, configuration);
boolean builtins = arguments.builtins; boolean builtins = arguments.builtins;
configuration.put(JVMConfigurationKeys.SCRIPT_PARAMETERS, arguments.script configuration.put(JVMConfigurationKeys.SCRIPT_PARAMETERS, arguments.script
@@ -122,15 +121,17 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
List<Module> modules = CompileEnvironmentUtil.loadModuleScript(arguments.module, messageCollector); List<Module> modules = CompileEnvironmentUtil.loadModuleScript(arguments.module, messageCollector);
messageCollector.setVerbose(oldVerbose); messageCollector.setVerbose(oldVerbose);
File directory = new File(arguments.module).getParentFile(); File directory = new File(arguments.module).getParentFile();
noErrors = KotlinToJVMBytecodeCompiler.compileModules(environment, modules, noErrors = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules,
directory, jar, outputDir, directory, jar, outputDir,
arguments.includeRuntime); arguments.includeRuntime);
} }
else if (arguments.script) { else if (arguments.script) {
List<String> scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size()); List<String> scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size());
JetCoreEnvironment environment = JetCoreEnvironment.createCoreEnvironmentForJVM(rootDisposable, configuration);
noErrors = KotlinToJVMBytecodeCompiler.compileAndExecuteScript(environment, scriptArgs); noErrors = KotlinToJVMBytecodeCompiler.compileAndExecuteScript(environment, scriptArgs);
} }
else { else {
JetCoreEnvironment environment = JetCoreEnvironment.createCoreEnvironmentForJVM(rootDisposable, configuration);
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, arguments.includeRuntime); noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, arguments.includeRuntime);
} }
return noErrors ? OK : COMPILATION_ERROR; return noErrors ? OK : COMPILATION_ERROR;
@@ -73,13 +73,12 @@ public class KotlinToJVMBytecodeCompiler {
} }
@Nullable @Nullable
public static ClassFileFactory compileModule(JetCoreEnvironment environment, Module moduleBuilder, File directory) { public static ClassFileFactory compileModule(CompilerConfiguration configuration, Module moduleBuilder, File directory) {
if (moduleBuilder.getSourceFiles().isEmpty()) { if (moduleBuilder.getSourceFiles().isEmpty()) {
throw new CompileEnvironmentException("No source files where defined"); throw new CompileEnvironmentException("No source files where defined");
} }
// TODO creating environment copy each time - not good. original environment shouldn't be passed at all CompilerConfiguration compilerConfiguration = configuration.copy();
CompilerConfiguration compilerConfiguration = environment.getConfiguration().copy();
for (String sourceFile : moduleBuilder.getSourceFiles()) { for (String sourceFile : moduleBuilder.getSourceFiles()) {
File source = new File(sourceFile); File source = new File(sourceFile);
if (!source.isAbsolute()) { if (!source.isAbsolute()) {
@@ -121,7 +120,7 @@ public class KotlinToJVMBytecodeCompiler {
} }
public static boolean compileModules( public static boolean compileModules(
JetCoreEnvironment environment, CompilerConfiguration configuration,
@NotNull List<Module> modules, @NotNull List<Module> modules,
@NotNull File directory, @NotNull File directory,
@Nullable File jarPath, @Nullable File jarPath,
@@ -129,7 +128,7 @@ public class KotlinToJVMBytecodeCompiler {
boolean jarRuntime) { boolean jarRuntime) {
for (Module moduleBuilder : modules) { for (Module moduleBuilder : modules) {
ClassFileFactory moduleFactory = compileModule(environment, moduleBuilder, directory); ClassFileFactory moduleFactory = compileModule(configuration, moduleBuilder, directory);
if (moduleFactory == null) { if (moduleFactory == null) {
return false; return false;
} }