|
| 1 | +package dotty.tools |
| 2 | +package dotc |
| 3 | +package fromtasty |
| 4 | + |
| 5 | +import scala.util.control.NonFatal |
| 6 | + |
| 7 | +import dotty.tools.io.Path |
| 8 | + |
| 9 | +import java.nio.file.{Files, Paths} |
| 10 | + |
| 11 | +object Debug { |
| 12 | + def main(args: Array[String]): Unit = { |
| 13 | + // Preload scala.util.control.NonFatal. Otherwise, when trying to catch a StackOverflowError, |
| 14 | + // we may try to load it but fail with another StackOverflowError and lose the original exception, |
| 15 | + // see <https://groups.google.com/forum/#!topic/scala-user/kte6nak-zPM>. |
| 16 | + val _ = NonFatal |
| 17 | + |
| 18 | + |
| 19 | + println("From tasty debug driver") |
| 20 | + assert(!args.contains("-d")) |
| 21 | + |
| 22 | + val fromSourcesOut = Files.createTempDirectory(Paths.get("out").toAbsolutePath, "from-sources-tmp") |
| 23 | + |
| 24 | + println(s"Compiling scala to sources to $fromSourcesOut") |
| 25 | + val compilation1 = dotc.Main.process("-d" +: fromSourcesOut.toString +: args) |
| 26 | + |
| 27 | + if (compilation1.hasErrors) { |
| 28 | + println("Failed compilation from sources") |
| 29 | + sys.exit(1) |
| 30 | + } |
| 31 | + |
| 32 | + val fromTastyOut = Files.createTempDirectory(Paths.get("out").toAbsolutePath, "from-tasty-tmp") |
| 33 | + |
| 34 | + val ext = "hasTasty" |
| 35 | + val classes = Path(fromSourcesOut).walk.filter(x => x.isFile && x.extension == ext).map { x => |
| 36 | + val source = x.toString |
| 37 | + source.substring(fromSourcesOut.toString.length + 1, source.length - ext.length - 1).replace('/', '.') |
| 38 | + }.toList |
| 39 | + |
| 40 | + val fromTastyArgs = |
| 41 | + "-from-tasty" :: insertClasspathInArgs(args.filterNot(_.endsWith(".scala")).toList, fromSourcesOut.toString) ::: classes |
| 42 | + |
| 43 | + println(s"Compiling TASTY to sources from $fromSourcesOut to $fromTastyOut") |
| 44 | + val compilation2 = dotc.Main.process(fromTastyArgs.toArray) |
| 45 | + |
| 46 | + if (compilation2.hasErrors) { |
| 47 | + println("Failed compilation from sources") |
| 48 | + sys.exit(1) |
| 49 | + } |
| 50 | + |
| 51 | + Path(fromSourcesOut).deleteRecursively() |
| 52 | + Path(fromTastyOut).deleteRecursively() |
| 53 | + } |
| 54 | + |
| 55 | + private def insertClasspathInArgs(args: List[String], cp: String): List[String] = { |
| 56 | + val (beforeCp, fromCp) = args.span(_ != "-classpath") |
| 57 | + val classpath = fromCp.drop(1).headOption.fold(cp)(_ + ":" + cp) |
| 58 | + "-classpath" :: classpath :: beforeCp ::: fromCp.drop(2) |
| 59 | + } |
| 60 | +} |
0 commit comments