diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java index f8699589476..e894f03582f 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java @@ -36,10 +36,7 @@ import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys; import org.jetbrains.kotlin.cli.common.ExitCode; import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments; import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants; -import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport; -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation; -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity; -import org.jetbrains.kotlin.cli.common.messages.MessageCollector; +import org.jetbrains.kotlin.cli.common.messages.*; import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsPackage; import org.jetbrains.kotlin.cli.jvm.compiler.CompilerJarLocator; import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles; @@ -84,16 +81,18 @@ public class K2JSCompiler extends CLICompiler { protected ExitCode doExecute( @NotNull K2JSCompilerArguments arguments, @NotNull Services services, - @NotNull final MessageCollector messageCollector, + @NotNull MessageCollector messageCollector, @NotNull Disposable rootDisposable ) { + final MessageSeverityCollector messageSeverityCollector = new MessageSeverityCollector(messageCollector); + if (arguments.freeArgs.isEmpty()) { - messageCollector.report(CompilerMessageSeverity.ERROR, "Specify at least one source file or directory", NO_LOCATION); + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, "Specify at least one source file or directory", NO_LOCATION); return ExitCode.INTERNAL_ERROR; } CompilerConfiguration configuration = new CompilerConfiguration(); - configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector); + configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageSeverityCollector); CompilerJarLocator locator = services.get(CompilerJarLocator.class); if (locator != null) { @@ -107,13 +106,22 @@ public class K2JSCompiler extends CLICompiler { Project project = environmentForJS.getProject(); List sourcesFiles = environmentForJS.getSourceFiles(); - if (arguments.verbose) { - reportCompiledSourcesList(messageCollector, sourcesFiles); + if (arguments.outputFile == null) { + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, "Specify output file via -output", CompilerMessageLocation.NO_LOCATION); + return ExitCode.INTERNAL_ERROR; } - if (arguments.outputFile == null) { - messageCollector.report(CompilerMessageSeverity.ERROR, "Specify output file via -output", CompilerMessageLocation.NO_LOCATION); - return ExitCode.INTERNAL_ERROR; + if (messageSeverityCollector.anyReported(CompilerMessageSeverity.ERROR)) { + return ExitCode.COMPILATION_ERROR; + } + + if (sourcesFiles.isEmpty()) { + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, "No source files", CompilerMessageLocation.NO_LOCATION); + return COMPILATION_ERROR; + } + + if (arguments.verbose) { + reportCompiledSourcesList(messageSeverityCollector, sourcesFiles); } File outputFile = new File(arguments.outputFile); @@ -122,14 +130,14 @@ public class K2JSCompiler extends CLICompiler { if (config.checkLibFilesAndReportErrors(new Function1() { @Override public Unit invoke(String message) { - messageCollector.report(CompilerMessageSeverity.ERROR, message, CompilerMessageLocation.NO_LOCATION); + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, message, CompilerMessageLocation.NO_LOCATION); return Unit.INSTANCE$; } })) { return COMPILATION_ERROR; } - AnalyzerWithCompilerReport analyzerWithCompilerReport = analyzeAndReportErrors(messageCollector, sourcesFiles, config); + AnalyzerWithCompilerReport analyzerWithCompilerReport = analyzeAndReportErrors(messageSeverityCollector, sourcesFiles, config); if (analyzerWithCompilerReport.hasErrors()) { return COMPILATION_ERROR; } @@ -142,7 +150,7 @@ public class K2JSCompiler extends CLICompiler { if (arguments.outputPrefix != null) { outputPrefixFile = new File(arguments.outputPrefix); if (!outputPrefixFile.exists()) { - messageCollector.report(CompilerMessageSeverity.ERROR, + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, "Output prefix file '" + arguments.outputPrefix + "' not found", CompilerMessageLocation.NO_LOCATION); return ExitCode.COMPILATION_ERROR; @@ -153,7 +161,7 @@ public class K2JSCompiler extends CLICompiler { if (arguments.outputPostfix != null) { outputPostfixFile = new File(arguments.outputPostfix); if (!outputPostfixFile.exists()) { - messageCollector.report(CompilerMessageSeverity.ERROR, + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, "Output postfix file '" + arguments.outputPostfix + "' not found", CompilerMessageLocation.NO_LOCATION); return ExitCode.COMPILATION_ERROR; @@ -171,7 +179,7 @@ public class K2JSCompiler extends CLICompiler { throw new RuntimeException(e); } - AnalyzerWithCompilerReport.reportDiagnostics(translationResult.getDiagnostics(), messageCollector); + AnalyzerWithCompilerReport.reportDiagnostics(translationResult.getDiagnostics(), messageSeverityCollector); if (!(translationResult instanceof TranslationResult.Success)) return ExitCode.COMPILATION_ERROR; @@ -179,7 +187,7 @@ public class K2JSCompiler extends CLICompiler { OutputFileCollection outputFiles = successResult.getOutputFiles(outputFile, outputPrefixFile, outputPostfixFile); if (outputFile.isDirectory()) { - messageCollector.report(CompilerMessageSeverity.ERROR, + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, "Cannot open output file '" + outputFile.getPath() + "': is a directory", CompilerMessageLocation.NO_LOCATION); return ExitCode.COMPILATION_ERROR; @@ -189,7 +197,7 @@ public class K2JSCompiler extends CLICompiler { if (outputDir == null) { outputDir = outputFile.getAbsoluteFile().getParentFile(); } - OutputUtilsPackage.writeAll(outputFiles, outputDir, messageCollector); + OutputUtilsPackage.writeAll(outputFiles, outputDir, messageSeverityCollector); return OK; } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.java index 86e67cb9c01..4fab113b481 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.java @@ -72,15 +72,16 @@ public class K2JVMCompiler extends CLICompiler { @NotNull MessageCollector messageCollector, @NotNull Disposable rootDisposable ) { + MessageSeverityCollector messageSeverityCollector = new MessageSeverityCollector(messageCollector); KotlinPaths paths = arguments.kotlinHome != null ? new KotlinPathsFromHomeDir(new File(arguments.kotlinHome)) : PathUtil.getKotlinPathsForCompiler(); - messageCollector.report(CompilerMessageSeverity.LOGGING, + messageSeverityCollector.report(CompilerMessageSeverity.LOGGING, "Using Kotlin home directory " + paths.getHomePath(), CompilerMessageLocation.NO_LOCATION); - final CompilerConfiguration configuration = new CompilerConfiguration(); - configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector); + CompilerConfiguration configuration = new CompilerConfiguration(); + configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageSeverityCollector); if (IncrementalCompilation.ENABLED) { IncrementalCacheProvider incrementalCacheProvider = services.get(IncrementalCacheProvider.class); @@ -100,7 +101,7 @@ public class K2JVMCompiler extends CLICompiler { } } catch (Throwable t) { - MessageCollectorUtil.reportException(messageCollector, t); + MessageCollectorUtil.reportException(messageSeverityCollector, t); return INTERNAL_ERROR; } @@ -109,21 +110,21 @@ public class K2JVMCompiler extends CLICompiler { } catch (PluginCliOptionProcessingException e) { String message = e.getMessage() + "\n\n" + PluginPackage.cliPluginUsageString(e.getPluginId(), e.getOptions()); - messageCollector.report(CompilerMessageSeverity.ERROR, message, CompilerMessageLocation.NO_LOCATION); + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, message, CompilerMessageLocation.NO_LOCATION); return INTERNAL_ERROR; } catch (CliOptionProcessingException e) { - messageCollector.report(CompilerMessageSeverity.ERROR, e.getMessage(), CompilerMessageLocation.NO_LOCATION); + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, e.getMessage(), CompilerMessageLocation.NO_LOCATION); return INTERNAL_ERROR; } catch (Throwable t) { - MessageCollectorUtil.reportException(messageCollector, t); + MessageCollectorUtil.reportException(messageSeverityCollector, t); return INTERNAL_ERROR; } if (arguments.script) { if (arguments.freeArgs.isEmpty()) { - messageCollector.report(CompilerMessageSeverity.ERROR, "Specify script source path to evaluate", + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, "Specify script source path to evaluate", CompilerMessageLocation.NO_LOCATION); return COMPILATION_ERROR; } @@ -154,7 +155,7 @@ public class K2JVMCompiler extends CLICompiler { putAdvancedOptions(configuration, arguments); - messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment", + messageSeverityCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment", CompilerMessageLocation.NO_LOCATION); try { configureEnvironment(configuration, arguments); @@ -175,11 +176,11 @@ public class K2JVMCompiler extends CLICompiler { final KotlinCoreEnvironment environment; if (arguments.module != null) { - MessageCollector sanitizedCollector = new FilteringMessageCollector(messageCollector, in(CompilerMessageSeverity.VERBOSE)); + MessageCollector sanitizedCollector = new FilteringMessageCollector(messageSeverityCollector, in(CompilerMessageSeverity.VERBOSE)); ModuleScriptData moduleScript = CompileEnvironmentUtil.loadModuleDescriptions(arguments.module, sanitizedCollector); if (outputDir != null) { - messageCollector.report(CompilerMessageSeverity.WARNING, + messageSeverityCollector.report(CompilerMessageSeverity.WARNING, "The '-d' option with a directory destination is ignored because '-module' is specified", CompilerMessageLocation.NO_LOCATION); } @@ -200,6 +201,16 @@ public class K2JVMCompiler extends CLICompiler { } else { environment = createCoreEnvironment(rootDisposable, configuration); + + if (messageSeverityCollector.anyReported(CompilerMessageSeverity.ERROR)) { + return COMPILATION_ERROR; + } + + if (environment.getSourceFiles().isEmpty()) { + messageSeverityCollector.report(CompilerMessageSeverity.ERROR, "No source files", CompilerMessageLocation.NO_LOCATION); + return COMPILATION_ERROR; + } + KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, arguments.includeRuntime); } @@ -213,7 +224,7 @@ public class K2JVMCompiler extends CLICompiler { return OK; } catch (CompilationException e) { - messageCollector.report(CompilerMessageSeverity.EXCEPTION, OutputMessageUtil.renderException(e), + messageSeverityCollector.report(CompilerMessageSeverity.EXCEPTION, OutputMessageUtil.renderException(e), MessageUtil.psiElementToMessageLocation(e.getElement())); return INTERNAL_ERROR; } diff --git a/compiler/tests/org/jetbrains/kotlin/test/MockLibraryUtil.java b/compiler/tests/org/jetbrains/kotlin/test/MockLibraryUtil.java index d74a378bf64..f4904f60360 100644 --- a/compiler/tests/org/jetbrains/kotlin/test/MockLibraryUtil.java +++ b/compiler/tests/org/jetbrains/kotlin/test/MockLibraryUtil.java @@ -73,7 +73,11 @@ public class MockLibraryUtil { File contentDir = JetTestUtils.tmpDir("testLibrary-" + jarName); File classesDir = new File(contentDir, "classes"); - compileKotlin(sourcesPath, classesDir, extraClasspath); + + List kotlinFiles = FileUtil.findFilesByMask(Pattern.compile(".*\\.kt"), new File(sourcesPath)); + if (!kotlinFiles.isEmpty()) { + compileKotlin(sourcesPath, classesDir, extraClasspath); + } List javaFiles = FileUtil.findFilesByMask(Pattern.compile(".*\\.java"), new File(sourcesPath)); if (!javaFiles.isEmpty()) {