Pass CompilerConfiguration to CLICompiler#doExecute
Extract common CompilerConfiguration setup code to a separate method
This commit is contained in:
@@ -31,6 +31,8 @@ import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments;
|
|||||||
import org.jetbrains.kotlin.cli.common.messages.*;
|
import org.jetbrains.kotlin.cli.common.messages.*;
|
||||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
|
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.CompileEnvironmentException;
|
import org.jetbrains.kotlin.cli.jvm.compiler.CompileEnvironmentException;
|
||||||
|
import org.jetbrains.kotlin.cli.jvm.compiler.CompilerJarLocator;
|
||||||
|
import org.jetbrains.kotlin.config.CompilerConfiguration;
|
||||||
import org.jetbrains.kotlin.config.Services;
|
import org.jetbrains.kotlin.config.Services;
|
||||||
import org.jetbrains.kotlin.progress.CompilationCanceledException;
|
import org.jetbrains.kotlin.progress.CompilationCanceledException;
|
||||||
import org.jetbrains.kotlin.progress.CompilationCanceledStatus;
|
import org.jetbrains.kotlin.progress.CompilationCanceledStatus;
|
||||||
@@ -166,6 +168,13 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
|
|||||||
reportUnknownExtraFlags(messageCollector, arguments);
|
reportUnknownExtraFlags(messageCollector, arguments);
|
||||||
|
|
||||||
GroupingMessageCollector groupingCollector = new GroupingMessageCollector(messageCollector);
|
GroupingMessageCollector groupingCollector = new GroupingMessageCollector(messageCollector);
|
||||||
|
|
||||||
|
CompilerConfiguration configuration = new CompilerConfiguration();
|
||||||
|
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, groupingCollector);
|
||||||
|
|
||||||
|
setupCommonArgumentsAndServices(configuration, arguments, services);
|
||||||
|
setupPlatformSpecificArgumentsAndServices(configuration, arguments, services);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ExitCode exitCode = OK;
|
ExitCode exitCode = OK;
|
||||||
|
|
||||||
@@ -187,7 +196,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
|
|||||||
}
|
}
|
||||||
Disposable rootDisposable = Disposer.newDisposable();
|
Disposable rootDisposable = Disposer.newDisposable();
|
||||||
try {
|
try {
|
||||||
ExitCode code = doExecute(arguments, services, groupingCollector, rootDisposable);
|
ExitCode code = doExecute(arguments, configuration, rootDisposable);
|
||||||
exitCode = groupingCollector.hasErrors() ? COMPILATION_ERROR : code;
|
exitCode = groupingCollector.hasErrors() ? COMPILATION_ERROR : code;
|
||||||
}
|
}
|
||||||
catch (CompilationCanceledException e) {
|
catch (CompilationCanceledException e) {
|
||||||
@@ -221,6 +230,19 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void setupCommonArgumentsAndServices(
|
||||||
|
@NotNull CompilerConfiguration configuration, @NotNull CommonCompilerArguments arguments, @NotNull Services services
|
||||||
|
) {
|
||||||
|
CompilerJarLocator locator = services.get(CompilerJarLocator.class);
|
||||||
|
if (locator != null) {
|
||||||
|
configuration.put(CLIConfigurationKeys.COMPILER_JAR_LOCATOR, locator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void setupPlatformSpecificArgumentsAndServices(
|
||||||
|
@NotNull CompilerConfiguration configuration, @NotNull A arguments, @NotNull Services services
|
||||||
|
);
|
||||||
|
|
||||||
private void reportUnknownExtraFlags(@NotNull MessageCollector collector, @NotNull A arguments) {
|
private void reportUnknownExtraFlags(@NotNull MessageCollector collector, @NotNull A arguments) {
|
||||||
for (String flag : arguments.unknownExtraFlags) {
|
for (String flag : arguments.unknownExtraFlags) {
|
||||||
collector.report(
|
collector.report(
|
||||||
@@ -234,8 +256,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
|
|||||||
@NotNull
|
@NotNull
|
||||||
protected abstract ExitCode doExecute(
|
protected abstract ExitCode doExecute(
|
||||||
@NotNull A arguments,
|
@NotNull A arguments,
|
||||||
@NotNull Services services,
|
@NotNull CompilerConfiguration configuration,
|
||||||
@NotNull MessageCollector messageCollector,
|
|
||||||
@NotNull Disposable rootDisposable
|
@NotNull Disposable rootDisposable
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation;
|
|||||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity;
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity;
|
||||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
||||||
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
|
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.CompilerJarLocator;
|
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||||
import org.jetbrains.kotlin.config.CompilerConfiguration;
|
import org.jetbrains.kotlin.config.CompilerConfiguration;
|
||||||
@@ -80,11 +79,10 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
protected ExitCode doExecute(
|
protected ExitCode doExecute(
|
||||||
@NotNull K2JSCompilerArguments arguments,
|
@NotNull K2JSCompilerArguments arguments, @NotNull CompilerConfiguration configuration, @NotNull Disposable rootDisposable
|
||||||
@NotNull Services services,
|
|
||||||
@NotNull final MessageCollector messageCollector,
|
|
||||||
@NotNull Disposable rootDisposable
|
|
||||||
) {
|
) {
|
||||||
|
final MessageCollector messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY);
|
||||||
|
|
||||||
if (arguments.freeArgs.isEmpty()) {
|
if (arguments.freeArgs.isEmpty()) {
|
||||||
if (arguments.version) {
|
if (arguments.version) {
|
||||||
return OK;
|
return OK;
|
||||||
@@ -93,14 +91,6 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
|||||||
return COMPILATION_ERROR;
|
return COMPILATION_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
CompilerConfiguration configuration = new CompilerConfiguration();
|
|
||||||
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
|
|
||||||
|
|
||||||
CompilerJarLocator locator = services.get(CompilerJarLocator.class);
|
|
||||||
if (locator != null) {
|
|
||||||
configuration.put(CLIConfigurationKeys.COMPILER_JAR_LOCATOR, locator);
|
|
||||||
}
|
|
||||||
|
|
||||||
ContentRootsKt.addKotlinSourceRoots(configuration, arguments.freeArgs);
|
ContentRootsKt.addKotlinSourceRoots(configuration, arguments.freeArgs);
|
||||||
KotlinCoreEnvironment environmentForJS =
|
KotlinCoreEnvironment environmentForJS =
|
||||||
KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JS_CONFIG_FILES);
|
KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JS_CONFIG_FILES);
|
||||||
@@ -244,6 +234,13 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
|||||||
return analyzerWithCompilerReport;
|
return analyzerWithCompilerReport;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setupPlatformSpecificArgumentsAndServices(
|
||||||
|
@NotNull CompilerConfiguration configuration, @NotNull K2JSCompilerArguments arguments, @NotNull Services services
|
||||||
|
) {
|
||||||
|
// No platform-specific arguments yet
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static Config getConfig(@NotNull K2JSCompilerArguments arguments, @NotNull Project project) {
|
private static Config getConfig(@NotNull K2JSCompilerArguments arguments, @NotNull Project project) {
|
||||||
if (arguments.target != null) {
|
if (arguments.target != null) {
|
||||||
|
|||||||
@@ -24,7 +24,10 @@ import org.jetbrains.kotlin.cli.common.ExitCode
|
|||||||
import org.jetbrains.kotlin.cli.common.ExitCode.*
|
import org.jetbrains.kotlin.cli.common.ExitCode.*
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
||||||
import org.jetbrains.kotlin.cli.common.messages.*
|
import org.jetbrains.kotlin.cli.common.messages.*
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.*
|
import org.jetbrains.kotlin.cli.jvm.compiler.CompileEnvironmentUtil
|
||||||
|
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||||
|
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||||
|
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler
|
||||||
import org.jetbrains.kotlin.cli.jvm.config.addJavaSourceRoot
|
import org.jetbrains.kotlin.cli.jvm.config.addJavaSourceRoot
|
||||||
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
|
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
|
||||||
import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal
|
import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal
|
||||||
@@ -46,7 +49,9 @@ import java.lang.management.ManagementFactory
|
|||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
||||||
override fun doExecute(arguments: K2JVMCompilerArguments, services: Services, messageCollector: MessageCollector, rootDisposable: Disposable): ExitCode {
|
override fun doExecute(arguments: K2JVMCompilerArguments, configuration: CompilerConfiguration, rootDisposable: Disposable): ExitCode {
|
||||||
|
val messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
|
||||||
|
|
||||||
val paths = if (arguments.kotlinHome != null)
|
val paths = if (arguments.kotlinHome != null)
|
||||||
KotlinPathsFromHomeDir(File(arguments.kotlinHome))
|
KotlinPathsFromHomeDir(File(arguments.kotlinHome))
|
||||||
else
|
else
|
||||||
@@ -55,17 +60,6 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
|||||||
messageCollector.report(CompilerMessageSeverity.LOGGING, "Using Kotlin home directory " + paths.homePath, CompilerMessageLocation.NO_LOCATION)
|
messageCollector.report(CompilerMessageSeverity.LOGGING, "Using Kotlin home directory " + paths.homePath, CompilerMessageLocation.NO_LOCATION)
|
||||||
PerformanceCounter.setTimeCounterEnabled(arguments.reportPerf)
|
PerformanceCounter.setTimeCounterEnabled(arguments.reportPerf)
|
||||||
|
|
||||||
val configuration = CompilerConfiguration()
|
|
||||||
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
|
|
||||||
|
|
||||||
if (IncrementalCompilation.isEnabled()) {
|
|
||||||
val incrementalCompilationComponents = services.get(IncrementalCompilationComponents::class.java)
|
|
||||||
configuration.put(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS, incrementalCompilationComponents)
|
|
||||||
}
|
|
||||||
|
|
||||||
val locator = services.get(CompilerJarLocator::class.java)
|
|
||||||
configuration.put(CLIConfigurationKeys.COMPILER_JAR_LOCATOR, locator)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!arguments.noJdk) {
|
if (!arguments.noJdk) {
|
||||||
configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots())
|
configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots())
|
||||||
@@ -223,6 +217,17 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun setupPlatformSpecificArgumentsAndServices(
|
||||||
|
configuration: CompilerConfiguration, arguments: K2JVMCompilerArguments, services: Services
|
||||||
|
) {
|
||||||
|
if (IncrementalCompilation.isEnabled()) {
|
||||||
|
configuration.put(
|
||||||
|
JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS,
|
||||||
|
services.get(IncrementalCompilationComponents::class.java)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow derived classes to add additional command line arguments
|
* Allow derived classes to add additional command line arguments
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user