Skip to content

Commit 19c13f3

Browse files
committed
Add -Yfrom-tasty to debug from TASTY compilation direct from sources
1 parent aa6459e commit 19c13f3

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

project/Build.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,13 @@ object Build {
630630
val dottyLib = packageAll.value("dotty-library")
631631
val args0: List[String] = spaceDelimited("<arg>").parsed.toList
632632
val decompile = args0.contains("-decompile")
633-
val args = args0.filter(arg => arg != "-repl" || arg != "-decompile")
633+
val debugFromTasty = args0.contains("-Yfrom-tasty")
634+
val args = args0.filter(arg => arg != "-repl" && arg != "-decompile" && arg != "-Yfrom-tasty")
634635

635636
val main =
636637
if (repl) "dotty.tools.repl.Main"
637638
else if (decompile) "dotty.tools.dotc.decompiler.Main"
639+
else if (debugFromTasty) "dotty.tools.dotc.fromtasty.Debug"
638640
else "dotty.tools.dotc.Main"
639641

640642
val extraClasspath =

0 commit comments

Comments
 (0)