|
| 1 | +package io.bazel.rulesscala.scalac; |
| 2 | + |
| 3 | +import io.bazel.rulesscala.scalac.reporter.DepsTrackingReporter; |
| 4 | +import io.bazel.rulesscala.scalac.compileoptions.CompileOptions; |
| 5 | +import java.nio.file.Paths; |
| 6 | +import io.bazel.rulesscala.scalac.reporter.ProtoReporter; |
| 7 | +import scala.tools.nsc.reporters.ConsoleReporter; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.nio.file.Files; |
| 11 | + |
| 12 | +//Invokes Scala 2 compiler |
| 13 | +class ScalacInvoker{ |
| 14 | + |
| 15 | + public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] compilerArgs) |
| 16 | + throws IOException, Exception{ |
| 17 | + |
| 18 | + ReportableMainClass comp = new ReportableMainClass(ops); |
| 19 | + |
| 20 | + ScalacInvokerResults results = new ScalacInvokerResults(); |
| 21 | + |
| 22 | + results.startTime = System.currentTimeMillis(); |
| 23 | + try { |
| 24 | + comp.process(compilerArgs); |
| 25 | + } catch (Throwable ex) { |
| 26 | + if (ex.toString().contains("scala.reflect.internal.Types$TypeError")) { |
| 27 | + throw new RuntimeException("Build failure with type error", ex); |
| 28 | + } else if (ex.toString().contains("java.lang.StackOverflowError")) { |
| 29 | + throw new RuntimeException("Build failure with StackOverflowError", ex); |
| 30 | + } else if (isMacroException(ex)) { |
| 31 | + throw new RuntimeException("Build failure during macro expansion", ex); |
| 32 | + } else { |
| 33 | + throw ex; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + results.stopTime = System.currentTimeMillis(); |
| 38 | + |
| 39 | + ConsoleReporter reporter = (ConsoleReporter) comp.getReporter(); |
| 40 | + if (reporter instanceof ProtoReporter) { |
| 41 | + ProtoReporter protoReporter = (ProtoReporter) reporter; |
| 42 | + protoReporter.writeTo(Paths.get(ops.diagnosticsFile)); |
| 43 | + } |
| 44 | + |
| 45 | + if (reporter instanceof DepsTrackingReporter) { |
| 46 | + DepsTrackingReporter depTrackingReporter = (DepsTrackingReporter) reporter; |
| 47 | + depTrackingReporter.prepareReport(); |
| 48 | + depTrackingReporter.writeDiagnostics(ops.diagnosticsFile); |
| 49 | + } |
| 50 | + |
| 51 | + if (reporter.hasErrors()) { |
| 52 | + reporter.flush(); |
| 53 | + throw new RuntimeException("Build failed"); |
| 54 | + } |
| 55 | + |
| 56 | + return results; |
| 57 | + } |
| 58 | + |
| 59 | + public static boolean isMacroException(Throwable ex) { |
| 60 | + for (StackTraceElement elem : ex.getStackTrace()) { |
| 61 | + if (elem.getMethodName().equals("macroExpand")) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + } |
| 65 | + return false; |
| 66 | + } |
| 67 | +} |
0 commit comments