|
| 1 | +package dotty.tools |
| 2 | +package dotc |
| 3 | +package fromtasty |
| 4 | + |
| 5 | +import scala.util.control.NonFatal |
| 6 | + |
| 7 | +import dotty.tools.io.Directory |
| 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 | + assert(!args.contains("-d")) |
| 19 | + |
| 20 | + val outPath = Paths.get("out") |
| 21 | + Directory(outPath).createDirectory() |
| 22 | + |
| 23 | + val tmpOut = Files.createTempDirectory(outPath.toAbsolutePath, "from-tasty-tmp") |
| 24 | + |
| 25 | + val fromSourcesOut = Files.createDirectory(tmpOut.resolve("from-source")) |
| 26 | + |
| 27 | + println("Compiling from .scala sources") |
| 28 | + val compilation1 = dotc.Main.process("-d" +: fromSourcesOut.toString +: args) |
| 29 | + |
| 30 | + if (compilation1.hasErrors) { |
| 31 | + println("Failed compilation from sources") |
| 32 | + Directory(tmpOut).deleteRecursively() |
| 33 | + sys.exit(1) |
| 34 | + } |
| 35 | + |
| 36 | + val fromTastyOut = Files.createDirectory(tmpOut.resolve("from-tasty")) |
| 37 | + |
| 38 | + val ext = "hasTasty" |
| 39 | + val classes = Directory(fromSourcesOut).walk.filter(x => x.isFile && x.extension == ext).map { x => |
| 40 | + val source = x.toString |
| 41 | + // transform foo/bar/Baz.hasTasty into foo.bar.Baz |
| 42 | + source.substring(fromSourcesOut.toString.length + 1, source.length - ext.length - 1).replace('/', '.') |
| 43 | + }.toList |
| 44 | + |
| 45 | + val fromTastyArgs = { |
| 46 | + "-from-tasty" :: |
| 47 | + "-d" :: fromTastyOut.toString :: |
| 48 | + insertClasspathInArgs(args.filterNot(_.endsWith(".scala")).toList, fromSourcesOut.toString) ::: |
| 49 | + classes |
| 50 | + } |
| 51 | + |
| 52 | + println("Compiling TASTY from .class sources") |
| 53 | + val compilation2 = dotc.Main.process(fromTastyArgs.toArray) |
| 54 | + |
| 55 | + if (compilation2.hasErrors) { |
| 56 | + println("Failed compilation from TASTY") |
| 57 | + println("Compilation input: " + fromSourcesOut) |
| 58 | + // In this case we do not delete the generated class files to allow further debugging. |
| 59 | + // For example `dotc -decompile` on one of the intermediate class files. |
| 60 | + sys.exit(1) |
| 61 | + } |
| 62 | + |
| 63 | + Directory(tmpOut).deleteRecursively() |
| 64 | + } |
| 65 | + |
| 66 | + private def insertClasspathInArgs(args: List[String], cp: String): List[String] = { |
| 67 | + val (beforeCp, fromCp) = args.span(_ != "-classpath") |
| 68 | + val classpath = fromCp.drop(1).headOption.fold(cp)(_ + ":" + cp) |
| 69 | + "-classpath" :: classpath :: beforeCp ::: fromCp.drop(2) |
| 70 | + } |
| 71 | +} |
0 commit comments